Interoperability with IKVM.NET: Seamless Java Integration

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.

  1. A Java Virtual Machine implemented in .NET: It has a JVM developed using C#.NET, that provides facilities like byte-code translation and verification, class loading, etcetera.
  2. A .NET implementation of the Java class libraries: It basically uses the OpenJDK project for the implementations of the JDK libraries.
  3. Tools that enable Java and .NET interoperability:
    • IKVM.NET includes the following tools.
      • ikvm: Java Virtual Machine
      • ikvmc: Compiles Java Bytecode to CIL
      • ikvmstub: Generates Java stub classes from .NET assemblies

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

IKVM.NET ByteCode Compiler (ikvmc.exe)

This is a static compiler to compiles 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.

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

ikvm -jar myapp.jar

Generates mylib.jar, containing stubs for classes, interfaces, etcetera, defined in c:\lib\mylib.dll.

We can use IKVM.NET in two ways.

  • Dynamically: In this mode, Java classes and jars are used directly to execute Java applications on the .NET runtime. The full Java class loader model is supported in this mode.
  • Statically: In order to allow Java code to be used by .NET applications, it must be compiled down to a DLL and used directly. The assemblies can be referenced directly by the .NET applications and the "Java" objects can be used as if they were .NET objects. While the static mode does not support the full Java class loader mechanism, it is possible for statically-compiled code to create a class loader and load classes dynamically.

Uses for IKVM.NET

IKVM.NET is useful for various software development scenarios. Here is a sampling of some of the possibilities.

  1. Drop-in JVM: The IKVM application included with the distribution is a .NET implementation of a Java Virtual Machine. In many cases, you can use it as a drop-in replacement for Java. For example, instead of typing "java -jar myapp.jar" to run an application, you can type:
    ikvm -jar myapp.jar
  2. Use Java libraries in your .NET application: IKVM.NET includes ikvmc, a Java bytecode to .NET IL translator. If you have a Java library that you would like to use in a .NET application then run "ikvmc -target:library mylib.jar" to create "mylib.dll".
    • For example: the Apache FOP project is an open-source XSL-FO processor written in Java that is widely used to generate PDF documents from XML source. With IKVM.NET technology, Apache FOP can be used by any .NET application.
  3. Develop .NET applications in Java: IKVM provides a way for you to develop .NET applications in Java. Although IKVM.NET does not include a Java compiler for .NET, you can use any Java compiler to compile Java source code to JVM bytecode, then use "ikvmc -target:exe myapp.jar" to produce a .NET executable. You can even use .NET APIs in your Java code using the included ikvmstub application.

Example

Now let's see how to use a class file or Library created in Java in .NET. Here I am creating a class file and library in Java for displaying a welcome and goodbye message. We have the following code in the Java file.

package Msg;

public class Message {

    public static void WelcomeMsg(String msg) {
        System.out.println("Welcome" + msg);
    }

    public static void GoodByeMsg(String msg) {
        System.out.println("Good Bye" + msg);
    }
}

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

using Msg;

namespace MessageDemo {
    class Program {
        static void Main(string[] args) {
            Message.WelcomeMsg(" Abhishek");
            Console.WriteLine("Now you can start your work............");
            Message.GoodByeMsg(" Abhishek");
        }
    }
}

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


Similar Articles