Click here to Skip to main content
15,867,686 members
Articles / Containers / Virtual Machine

Interoperability between a Java Virtual Machine and the CLR

Rate me:
Please Sign up or sign in to vote.
2.84/5 (12 votes)
7 Jan 2009CPOL1 min read 36.8K   249   27   8
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.

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:

C#
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)


Written By
Software Developer (Senior)
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionMy vote of 5 Pin
Massimo Conti19-Apr-13 8:35
Massimo Conti19-Apr-13 8:35 
GeneralMy vote of 1 Pin
Vitaly Shelest12-Apr-09 21:22
Vitaly Shelest12-Apr-09 21:22 
GeneralMy vote of 1 Pin
Stan Alex8-Jan-09 1:13
Stan Alex8-Jan-09 1:13 
GeneralMy vote of 2 Pin
Tarabanko Yury7-Jan-09 23:48
Tarabanko Yury7-Jan-09 23:48 
Just an overview.
GeneralMy vote of 1 Pin
rcollina7-Jan-09 21:52
rcollina7-Jan-09 21:52 
GeneralMy vote of 2 Pin
Lionel LASKE6-Jan-09 2:57
Lionel LASKE6-Jan-09 2:57 
GeneralTechnical details Pin
Sohel_Rana31-Dec-08 16:45
Sohel_Rana31-Dec-08 16:45 
GeneralInteresting! [modified] Pin
Dinesh Mani30-Dec-08 17:39
Dinesh Mani30-Dec-08 17:39 

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.