Using Java Classes in your .NET Application -摘自网络

Introduction

Suppose you have been asked to migrate an existing multi-tier application to .NET where the business layer is written in Java. Normally you would have no option but to recode and port the entire application to any .NET language (e.g. C#). However this is where IKVM.NET comes to the rescue.

IKVM.NET is an open source implementation of Java for Mono /Microsoft .NET Framework and makes it possible both to develop .NET applications in Java, and to use existing Java API's and libraries in applications written in any .NET language. It is written in C# and the executables, documentation and source code can be downloaded from here.

IKVM.NET consists of the following three main parts:
A Java Virtual Machine implemented in .NET
A .NET implementation of the Java class libraries
Tools that enable Java and .NET interoperability

However before we get any further into this topic, let’s discuss about few of the main components of the IKVM.NET package which we would be using later in this article.
IKVM.Runtime.dll: The VM runtime and all supporting code containing the byte code JIT compiler/verifier, object model remapping infrastructure and the managed .NET re-implementations of the native methods in Classpath.
IKVM.GNU.Classpath.dll: Compiled version of GNU Classpath, the Free Software Foundation's implementation of the Java class libraries, plus some additional IKVM.NET specific code.
ikvm.exe: Starter executable, comparable to java.exe ("dynamic mode").
ikvmc.exe: Static compiler. Used to compile Java classes and jars into a .NET assembly ("static mode").

Now back to our problem of migrating the existing Java business classes so that they can be accessed by the newly proposed .NET application. We would also like to use the various existing Java API and libraries in our .NET application. Let’s start by doing just that.
Setting Up IKVM.NET

Download the binary distribution from the sourceforge site and unzip the contents to C:\ikvm (or X:\ikvm where X is your drive). You would find the ikvm executables and DLLs in the C:\ikvm\bin directory. Open a command or shell window, cd to C:\ikvm\bin, and type ‘ikvm’.

If your system is operating correctly, you should see the following output:

usage: ikvm [-options] <class> [args...] (to execute a class) or ikvm -jar [-options] <jarfile> [args...] (to execute a jar file)

For Linux based systems, the setup is similar as above. This is all you need to do for running the demo application.
Our Demo Java Business Class (JavaToNet.java)
Collapse | Copy Code
public class JavaToNet
{
public static void main(String[] args)
{
System.out.println("This is a demonstration Program which\n");
System.out.println("shows the conversion of Java class to\n");
System.out.println("a .NET dll\n");
}
public static double AddNumbers(double a,double b){
double c = 0;
c = a + b;
return c;
}
public static double SubNumbers(double a,double b){
double c = 0;
c = a - b;
return c;
}
public static double MulNumbers(double a,double b){
double c = 0;
c = a * b;
return c;
}
public static double DivNumbers(double a,double b){
double c = 0;
c = a / b;
return c;
}
}

Our Java class is very simple. It has four functions for add, subtract, multiply and divide that take two double values and return a result. Our objective is to access these functions through our C# application. Compile the above Java file to get the JavaToNet.class. We will use this Java class file to generate the .NET DLL to be referenced in our C# program.
Using IKVM.NET to Convert Java Class to .NET DLL

Copy the above Java class file (JavaToNet.class) to the C:\ikvm\bin directory. Now run the following command:


This would create the JavaToNet.dll from the JavaToNet.class file. There are other command line option for ikvmc.exe. For example: ‘ikvmc –target:exe javaToNet.class’ would create an EXE and not a DLL. You can get all the options by typing ‘ikvmc’ in the command line.
Setting Up Your .NET Development Environment


Start by creating a C# Windows application project.


Drag and drop controls into the form as shown:




Add the following DLLs as references to the project. Both DLLs are present in the C:\ikvm\bin folder.
JavaToNet.dll
IKVM.GNU.Classpath.dll


Add the following code to the button click event of the ‘Calculate’ button:
Collapse | Copy Code
private void btnCal_Click(object sender, System.EventArgs e)
{
if (rdAdd.Checked == true)
{
txtResult.Text = Convert.ToString(JavaToNet.AddNumbers
(Convert.ToDouble(txtNum1.Text),Convert.ToDouble(txtNum2.Text)));
}else if (rdSub.Checked ==true)
{
txtResult.Text = Convert.ToString(JavaToNet.SubNumbers
(Convert.ToDouble(txtNum1.Text),Convert.ToDouble(txtNum2.Text)));
}
else if (rdMul.Checked == true)
{
txtResult.Text = Convert.ToString(JavaToNet.MulNumbers
(Convert.ToDouble(txtNum1.Text),Convert.ToDouble(txtNum2.Text)));
}
else
{
txtResult.Text = Convert.ToString(JavaToNet.DivNumbers
(Convert.ToDouble(txtNum1.Text),Convert.ToDouble(txtNum2.Text)));
}
}


Add the following using directive on the top of the *.cs file:
Collapse | Copy Code
using TimeZone = java.util.TimeZone;


Add the following code to the button click event of the ‘Time Zone’ button.
Collapse | Copy Code
private void btnTimeZone_Click(object sender, System.EventArgs e)
{
MessageBox.Show(TimeZone.getDefault().getDisplayName());
}


Compile and run the application. The C# application would now call the AddNumbers(), SubNumbers(), MulNumbers() and DivNumbers() functions present in the JavaToNet.dll and return the result.


Click on the ‘Time Zone’ button. The application accesses the java.util.TimeZone class and displays the exact time zone of the place.


Conclusion

Since these methods had originally been written in Java, IKVM.NET provides us an easy and viable way to access those classes and methods from a .NET application. Similarly as shown above in the ‘Time Zone’ example, you can access most of the existing Java packages (e.g. java.io, java.util, etc.) and use them in your application.

However there are certain drawbacks. IKVM.NET, while still being actively developed, has limited support for AWT classes and hence porting Java GUI can be ruled out at present. Also some of the default Java classes are still being ported so you might not get all the functionalities you require. Also if your application depends on the exact Java class loading semantics, you might have to modify it to suit your needs.
History
03-25-06 Initial publication
License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
posted @ 2012-07-03 10:18  iDEAAM  阅读(241)  评论(0编辑  收藏  举报