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.
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.
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
History
- 30th December, 2008: Initial post
- 6th January, 2009: Added source code and images to article