Click here to Skip to main content
13,002,163 members (75,477 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked
Posted 23 Mar 2006

Using Java Classes in your .NET Application

, 23 Mar 2006
Rate this:
Please Sign up or sign in to vote.
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 APIs and libraries in applications written in any .NET language.


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:

  1. A Java Virtual Machine implemented in .NET
  2. A .NET implementation of the Java class libraries
  3. 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 (

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

  1. Start by creating a C# Windows application project. 

  2. Drag and drop controls into the form as shown:

  3. 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
  4. Add the following code to the button click event of the ‘Calculate’ button:

    private void btnCal_Click(object sender, System.EventArgs e)
    if (rdAdd.Checked == true)
        txtResult.Text = Convert.ToString(JavaToNet.AddNumbers
        }else if (rdSub.Checked ==true)
        txtResult.Text = Convert.ToString(JavaToNet.SubNumbers
        else if (rdMul.Checked == true)
        txtResult.Text = Convert.ToString(JavaToNet.MulNumbers
        txtResult.Text = Convert.ToString(JavaToNet.DivNumbers
  5. Add the following using directive on the top of the *.cs file:

    using TimeZone = java.util.TimeZone;
  6. Add the following code to the button click event of the ‘Time Zone’ button.

    private void btnTimeZone_Click(object sender, System.EventArgs e)
  7. 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.

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


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.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.


  • 03-25-06 Initial publication


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
Chayan Ray has been working as a Technical Consultant in a CMM level 5 company in India. His technical domain includes ASP.NET, C#, PHP, Perl, Cold Fusion, MySQL and MSSQL 2000.

You may also be interested in...


Comments and Discussions

QuestionIs it possible to execute java application (which is dependent on many 3rd party libs) from C# code using JNI based tools likw IKVM? Pin
Member 1168666312-May-15 21:54
memberMember 1168666312-May-15 21:54 
QuestionError in generate dll -warning IKVMC0101: Unable to compile class- (class format error "52.0") Help please!!! Pin
Member 1137849719-Jan-15 12:01
memberMember 1137849719-Jan-15 12:01 
AnswerRe: Error in generate dll -warning IKVMC0101: Unable to compile class- (class format error "52.0") Help please!!! Pin
Member 932290416-Nov-15 4:14
memberMember 932290416-Nov-15 4:14 
GeneralRe: Error in generate dll -warning IKVMC0101: Unable to compile class- (class format error "52.0") Help please!!! Pin
Member 1306274319-Apr-17 19:22
memberMember 1306274319-Apr-17 19:22 
Questionerrors Pin
Member 1111427527-Sep-14 7:37
memberMember 1111427527-Sep-14 7:37 
QuestionMissing file Pin
Shailenderrajput2-Feb-14 22:36
memberShailenderrajput2-Feb-14 22:36 
QuestionError Message ->IKVMC0002: Output File is "JavaToNet.dll" Pin
MrKyaw20-Jan-13 21:24
memberMrKyaw20-Jan-13 21:24 
Hi Mr Chayan,

I started to use your ikvmc as below

C:\ikvm-7.2.4630.5\bin>ikvmc -target:library JavaToNet.class
IKVM.NET Compiler version 7.2.4630.5
Copyright (C) 2002-2012 Jeroen Frijters

note IKVMC0002: Output file is "JavaToNet.dll"


After that I got JavaToNet.dll then add References in .NET project.

Another error came out as below
Consider app.config remapping of assembly "IKVM.Runtime, Culture=neutral, PublicKeyToken=13235d27fcbfff58" from Version "" [] to Version "7.2.4630.5" [..\..\..\..\..\..\..\..\ikvm-7.2.4630.5\bin\IKVM.Runtime.dll] to solve conflict and get rid of warning.
C:\Windows\Microsoft.NET\Framework\v3.5\Microsoft.Common.targets : warning MSB3247: Found conflicts between different versions of the same dependent assembly.
C:\Windows\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /unsafe- /checked- /nowarn:1701,1702 /nostdlib- /errorreport:prompt /warn:4 /baseaddress:285212672 /define:DEBUG;TRACE /reference:bin\Debug\IKVM.GNU.Classpath.dll /reference:..\..\..\..\..\..\..\..\ikvm-7.2.4630.5\bin\JavaToNet.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Drawing.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Windows.Forms.dll /reference:C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Xml.dll /debug+ /debug:full /filealign:4096 /optimize- /out:obj\Debug\WindowsApplication2.exe /resource:obj\Debug\WindowsApplication2.Form1.resources /target:winexe /warnaserror- /win32icon:App.ico AssemblyInfo.cs Form1.cs
C:\InterOPs(Java&NET)\csharpikvm_src\application_src\NET\JavaToNet\Form1.cs(217,5): error CS0012: The type 'java.lang.Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'IKVM.OpenJDK.Core, Version=7.2.4630.5, Culture=neutral, PublicKeyToken=13235d27fcbfff58'.
c:\ikvm-7.2.4630.5\bin\JavaToNet.dll: (Related file)

It was working using your generated DLL in this .NET project and pls advise me.

Thanks and best regards
QuestionGetting errors while converting Jar to dll using IKVM - please help! Pin
sonam5021732-Jan-13 3:24
membersonam5021732-Jan-13 3:24 
QuestionGetting Class not found errors while converting jar to dll using IKVM - please help!!!! Pin
sonam5021732-Jan-13 3:23
membersonam5021732-Jan-13 3:23 
QuestionGetting Class not found errors while converting jar to dll using IKVM Pin
sonam5021732-Jan-13 3:22
membersonam5021732-Jan-13 3:22 
Questionikmv class java to .net c# Pin
Member 455975016-Jun-12 3:37
memberMember 455975016-Jun-12 3:37 
QuestionHow to use IKVM without creating dll Pin
NarVish25-May-12 3:29
memberNarVish25-May-12 3:29 
QuestionThank you... Pin
rajendranmca200824-Oct-11 21:42
memberrajendranmca200824-Oct-11 21:42 
QuestionThanks Pin
sanya.fesak21-Sep-11 19:12
membersanya.fesak21-Sep-11 19:12 
GeneralIKVM Framework is Updated Pin
majed.idrees16-Apr-11 16:20
membermajed.idrees16-Apr-11 16:20 
Questionthe architecture in IKVM ? Pin
Nguyen_Chuong21-Feb-11 4:54
memberNguyen_Chuong21-Feb-11 4:54 
GeneralJar to Native Dll Pin
dummy programmer241-Dec-10 15:13
memberdummy programmer241-Dec-10 15:13 
GeneralRe: Jar to Native Dll Pin
Chayan1-Dec-10 22:08
memberChayan1-Dec-10 22:08 
GeneralRe: Jar to Native Dll Pin
dummy programmer242-Dec-10 4:11
memberdummy programmer242-Dec-10 4:11 
GeneralMy vote of 5 Pin
DilushaM9-Jul-10 9:04
memberDilushaM9-Jul-10 9:04 
Generalusing a jar file with dependent jars Pin
IthilienX26-Oct-08 14:14
memberIthilienX26-Oct-08 14:14 
AnswerRe: using a jar file with dependent jars Pin
Rini Jackson14-Mar-12 5:20
memberRini Jackson14-Mar-12 5:20 
GeneralRe: using a jar file with dependent jars Pin
Nitin Sawant30-Nov-12 0:40
memberNitin Sawant30-Nov-12 0:40 
QuestionHas Dependent Jar files? Pin
MHSrinivasan19-Jun-08 0:15
memberMHSrinivasan19-Jun-08 0:15 
GeneralProblem with IKVM Pin
Askalo6-Jul-07 5:37
memberAskalo6-Jul-07 5:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170624.1 | Last Updated 24 Mar 2006
Article Copyright 2006 by Chayan
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid