Introduction
This article is all about IKVM.NET and how to use it. IKVM.NET is an open-source implementation of a JVM for the Microsoft .NET Framework and Mono. Suppose you have developed a library in Java and you want to use it in your .NET projects, then IKVM.NET helps to use Java libraries in .NET. Java and .NET are two major current technologies for software development and Java is an older technology and there has already been a substantial amount of work done. So when it comes to reusability then it is a very common practice to use a library created in Java in .NET.
Details
IKVM.NET includes the following three main components:
a. IKVM.NET Virtual Machine (ikvm.exe)
This is a starter executable. We can compare it to java.exe ("dynamic mode"). It loads a class file and executes its main method, if we pass the class file name as a parameter then it will execute Java code in an executable jar file. If we pass a jar file as a parameter then it will execute it.
Syntax for use
ikvm [ options ] classname [ args1,args2…..argsN]
ikvm [ options ] -jar jarfile [args1,args2…..argsN]
Example
ikvm -jar /usr/share/myprog.jar
Executes Java code in the executable jar file "/usr/share/myprog.jar".
b. IKVM.NET ByteCode Compiler (ikvmc.exe)
This is a static compiler to compile Java classes and jars into a .NET assembly ("static mode"). This tool converts Java bytecode to .NET DLLs and exes. It converts the Java bytecodes in the input files to a .NET DLL. So when we pass multiple jar files and class files it will combine them and generate a single exe or DLL file. Whether an exe or a DLL depends on whether the class file and jar file that is passed has a Main method. If they have a Main method then it will generate an exe otherwise DLL.
Syntax for use
ikvmc [ options ] classOrJarfile [ classOrJarfile ... ]
Example
ikvmc myProg.jar
Scans myprog.jar for the main method. If found, a .exe is produced; otherwise, a .dll is generated.
c. IKVM.NET Stub Generator (ikvmstub.exe)
A tool that generates stub class files from a .NET assembly, so that Java code can be compiled against .NET code. The ikvmstub tool generates Java stubs from .NET assemblies. ikvmstub reads the specified assembly and generates a Java jar file containing Java interfaces and stub classes. For more information about the generated stubs. The tool uses the following algorithm to locate the assembly:
First, it attempts to load the assembly from the default load context of ikvmstub.exe. For practical purposes, this usually means it searches the Global Assembly Cache.
If not found in the default load context then ikvmstub looks for the assembly at the indicated path (or the current directory, if no path is supplied).
Syntax for use
ikvmstub assemblyNameOrPath
Example
ikvmstub c:\lib\mylib.dll
Generates mylib.jar, containing stubs for classes, interfaces, etcetera, defined in c:\lib\mylib.dll.
We can use IKVM.NET in two ways:
Now compile this Java file into a class file, so we have a Message.class file. I have also created a Message.jar file because I will show how to use a class file or jar file to generate a DLL file using IKVM.NET. In the following way we can generate jar file from a class file:
jar cvf Message.jar Message.class
Now we need to generate the Message DLL from a class file or jar file. So we need to use IKVMC for this. You can download IKVM.NET from
here.
After downloading this zip file, when you unzip it, you will see a bin folder. Inside the bin folder, you can find all the necessary files. Now copy your class file or jar (Message.class or Message.jar) file to the bin folder. Open your command prompt and go to this bin folder by cd command.
Type in (one of) the following commands for generating the DLL file from the class file or jar file.
ikvmc -target:library Message.jar
ikvmc -target:library Message.class
You can use either one of the commands to generate a DLL file. Now you can find the Message.dll file inside the bin folder.
Create a console project in C#. Add a reference for Message.dll and IKVM.OpenJDK.Core.dll. Write the following code in your project to access the methods of this DLL.
Now you can run your project to get the following output:
Summary
IKVM.NET is a very interesting library for developers who know both C# and Java, they can understand how useful it is. If they have worked on one language and created some important library and they want to use it in another language then they can use it very easily using IKVM.NET.