Click here to Skip to main content
Click here to Skip to main content
Go to top

Interoperability between a Java Virtual Machine and the CLR

, 7 Jan 2009
Rate this:
Please Sign up or sign in to vote.
With IKVM, you can run compiled Java code (byte code) directly on Microsoft .NET or Mono. The byte code is converted on the fly to CIL and executed.

Introduction

IKVM.NET is an implementation of Java for Mono and the Microsoft .NET Framework. IKVM is free software, distributed under a permissive free software license.

IKVM includes the following components:

  • A Java Virtual Machine implemented in .NET
  • A .NET implementation of the Java class libraries
  • Tools that enable Java and .NET interoperability

With IKVM, you can run compiled Java code (byte code) directly on Microsoft .NET or Mono. The byte code is converted on the fly to CIL and executed.

Using IKVM

Step 1: Write a Java class, e.g., dllClass.java.

// File 'dllClass.java':
public class dllClass
{    
    public static String foo() {
        return("STPL Inc");
    }
    public static int add(int a, int b)
    {
        int sum = a + b;
        return sum;
    }
    public static int Mult(int a, int b)
    {
        int mul = a * b;
        return mul;
    }
}

Step 2: Compile it to get the class file dllClass.class.

javac dllClass.java

Step 3: Again, convert it to a jar file dllClass.jar.

jar cf dllClass.jar dllClass.class

Step 4: Now, copy the jar file and copy it to IKVM/bin.

Step 5: Open Command Prompt, and go to IKVM/bin.

Step 6: Type IKVM and press the Enter key.

IKVM.JPG

Step 7: Now, change the jar file to a DLL using this command:

Ikvmc dllClass.jar

We will now get the dllClass.dll file.

Step 8: Copy IKVM DLLs and dllClass.dll to the application bin directory.

Step 9: Add a reference to IKVM.OpenJDK.ClassLibrary.dll and dllClass.dll.

Step 10: Now, you can use this DLL in a C# code:

private void addbtn_Click(object sender, EventArgs e)
{
    int a = dllClass.add(Convert.ToInt32(textBox1.Text), 
            Convert.ToInt32(textBox2.Text));
    lblRs.Text = "Sum of " + textBox1.Text + 
                 " and " + textBox2.Text+" = "+a;
}

private void button1_Click(object sender, EventArgs e)
{
    int add = dllClass.Mult(Convert.ToInt32(textBox1.Text), 
                            Convert.ToInt32(textBox2.Text));
    lblRs.Text = "Multi. of " + textBox1.Text + " and " + 
                 textBox2.Text + " = " + add;
}

private void button2_Click(object sender, EventArgs e)
{
    string  a = dllClass.foo();
    lblRs.Text = a;
}

Result Screens

Screen1.JPG

Screen2.JPG

History

  • 30th December, 2008: Initial post
  • 6th January, 2009: Added source code and images to article

License

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

Share

About the Author

Vivek Srivastava SSE
Software Developer (Senior) CloudXtension IT Solution, Gurgaon
India India
No Biography provided

Comments and Discussions

 
QuestionMy vote of 5 PinmemberMassimo Conti19-Apr-13 8:35 
GeneralMy vote of 1 PinmemberVitaly Shelest12-Apr-09 21:22 
GeneralMy vote of 1 PinmemberStan Alex8-Jan-09 1:13 
GeneralMy vote of 2 PinmemberTarabanko Yury7-Jan-09 23:48 
GeneralMy vote of 1 PinmemberRoberto Collina7-Jan-09 21:52 
GeneralMy vote of 2 PinmemberLionel LASKE6-Jan-09 2:57 
GeneralTechnical details PinmemberSohel_Rana31-Dec-08 16:45 
GeneralInteresting! [modified] PinmemberDinesh Mani30-Dec-08 17:39 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 7 Jan 2009
Article Copyright 2008 by Vivek Srivastava SSE
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid