How to use IKVM to integrate Java to DotNet

Using Ja.Net to integrate Java and DotNet(c#): Basic

Using Ja.Net to integrate Java and DotNet(c#): Class and Type

Using Ja.Net to integrate Java and DotNet(c#): More detailed basic issues

Using Ja.Net to integrate Java and DotNet(c#): Encoding and Properties

Using Ja.Net to integrate Java and DotNet(c#): Class implementing and inheriting

Using Ja.Net to integrate Java and DotNet(c#): Assemblies in Java Source code

Using Ja.Net to integrate Java and DotNet(c#): IO in Java and DotNet

In this chapter, I will discuss more detailed information of the basic compiling and executing using Ja.Net to integrate Java and C#. The chapter covers the following topics:

  1. How to compile the source code with references.
  2. How to deploy applications mixed Java and C#.
  3. How to convert a java.lang.Class to System.Type.
  4. Is java.lang.Class inherited from “System.Type” in Ja.Net.

Reference

As I have mentioned in the preceding chapter, I described that if you want to refer some DLL used by Java, you should add a –cp parameter to indicate the referring. In fact, the Ja.net only add “System.Core.dll” and “System.dll” into the default compile environment, so if you want to use others DLLs, even those DLLs are in system-folder, you have to add the references manually.

If you want use the DLLs which generated by Ja.Net into some DotNet project, basically, you must add the following DLLs:

  • JanetVMM.dll
  • janet.luni.dll
  • janet.security.dll

All of the libraries required for compiling and running reside in the folder “%JA.NET Installation directory%/jre/bin/default”.

Deploy applications mixed Java and C#.

That’s is a great issue that we don’t know how to deploy applications with Ja.Net, it means we don’t assure that the applications should work well in the customers’ machine as we wishing after development done.

In this case, IKVM.Net is definitely better than Ja.Net, it doesn’t need any extra libraries to execute “Java code”. Ja.net does need a runtime environment to fulfill the requirement, we call it “JVM”. It does mean that we can not deploy this “JVM” as some libraries/DLLs with applications, the customers have to install a new “JVM” by MSI or manually.

The best way is customers use MSI to install the JVM.

Here is a script describe how to install the JVM manually:

http://www.janetdev.org/ManualJaNETSEInstallation/tabid/91/Default.aspx

The JVM required:

  • DotNet framework V3.5 or above
  • Visual C++ 2008 runtime libraries

You can download JVM MSI from:

http://www.janetdev.org/Downloads/tabid/55/Default.aspx

After my testing, only JDK version (not JRE, not JDDK) works in Windows XP.

My operational system is: Windows XP SP3, DotNet framework V3.5, Visual C++ 2008 runtime.

To be summarized, it is a heavy solution that using Ja.Net to integrate Java and DotNet, if it is not necessary using the features of JDK1.5 or above, I suggest you using IKVM.Net to do the integration.

For my SQLReport, I have to use Ja.Net, because so many source code in JDK1.5 features.

How to convert a java.lang.Class to System.Type

I am so sorry that I have made some mistakes in the previous chapter about Class and Type, I said:

  • Java can create a object instance by Class.forName, the names generated by DotNet language are in the same namespace which Java is using.
  • The DotNet type “System.Type” is not a sub-class of “java.lang.Class”, it can not be treat as a instance of “java.lang.Class”, so we can not use the DotNet style invoking “GetType” to retrieve the type of some objects created by DotNet.
  • In Java source code, the DotNet primitive types are sub-class of “System.Type”, it is quite different with the primitive types in Java.
  • The Root objects in Java and in DotNet are similar.
  • In Java source code, DotNet Type.GetType() can create classes which generated by DotNet.
  • In Java source code, DotNet Type.GetType() CAN NOT create classes which generated by Java.

I missed following issues:

  1. The type java.lang.Class has been modified by Ja.Net, it add a new method whose sign is “System.Type toType()”, you can convert a java.lang.class to a “System.Type” instance.
  2. System.Type.GetType() can only found the classes/types defined in C#, no matter in Java source code or in C# source code.
  3. If you want convert a System.Type instance into a java.lang.Class instance, the only way is using “java.lang.Class.forName(system_type.FullName)”。
  4. Ja.Net create some fake classes with same name in C#, “Type” is one of those (I describe it detailed in the following content).

Is java.lang.Class inherited from “System.Type” in Ja.Net.

Before I give the conclusion, let’s check some interesting things. There are some snapshots from my Visual studio 2008, the version is:

NF30002

OK, let’s check what happened when I inspecting the type “java.lang.Class” (click to enlarge the picture.)

NF30000

From this picture, VS2008 indicates that the class “java.lang.Class” is inherited from “System.Type”, but unfortunately, the following code-snippet can not be compiled successfully:

java.lang.Class class=….

System.Type type=(System.Type)class;//class as System.Type

Why?

Eventually, I have to check the byte-code of janet.

NF30001

It is surprised that “java.lang.Class” is inherited from “java.lang.reflect.Type” rather that what shown in the VS2008. I don’t know why Vs2008 made such a mistake.


Jeason Zhao (沈胜衣,斛律光) ------雪饮再现,一个人的江湖
我知道我是谁,我是沈胜衣,默默的活着,就像空气。