Using Java classes in your. NET application-from Network

Source: Internet
Author: User
Introduction

Suppose you have been asked to migrate an existing multi-tier Application. net where the business layer is written in Java. normally you wowould 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 wocould 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. 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 wowould 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 wocould 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 abve. 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
{< br> Public static void main (string [] ARGs)
{< br> system. out. println ("this is a demonstration program which \ n");
system. out. println ("shows the conversion of Java class to \ n");
system. out. println (". net dll \ n ");
}< br> Public static double addnumbers (double A, double B) {
double C = 0;
C = a + B;
return C;
}< br> Public static double subnumbers (double A, double B) {
double C = 0;
C = A-B;
return C;
}< br> Public static double mulnumbers (double, double B) {
double C = 0;
C = a * B;
return C;
}< br> Public static double divnumbers (double A, double B) {
double C = 0;
C = A/B;
return C;
}< BR >}

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. 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 woshould 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 'wocould 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 wocould 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. 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.