IKVM.NET Tutorial |
- Setup your Environment
- Run a Java Application Dynamically
- Convert a Java Application to .NET
- Develop a .NET Application in Java
Setup your Environment
This tutorial includes information for both the Windows and Linux platforms. It assumes that Windows users will be using the .NET SDK, and Linux users will be using the Mono SDK.
This tutorial references files in the samples distribution available on the download page. Before you begin, prepare your environment by adding the following to your PATH environment variable:
- The directory containing the IKVM executables
- The directory containing the C# compiler (Windows: csc / Mono: mcs). On Windows, this is typically C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322.
- The directory containing a Java compiler (javac or jikes)
Run a Java Application Dynamically
IKVM.NET includes a Java Virtual Machine implented in C#. To try it out, navigate to IKVMROOT\samples\hello and compile the sample application:
Now, to run the application using the IKVM Virtual Machine, enter the following:
This command starts the virtual machine, which searches for a file named Hello.class. When it finds it, it loads it and executes the bytecodes dynamically. You should be prompted to enter your name, and see a brief greeting.
If you experience problems, check the following:
- Check your capitalization: ikvm, like java, requires you to capitalize class names correctly.
- If ikvm reports a ClassNotFoundException, check whether the CLASSPATH environment variable is set. If so, try clearing the CLASSPATH or adding the current directory to it so ikvm can find the class in the current directory.
You can also execute Java applications in a jar file. Try it out:
See the ikvm reference for more information about ikvm command line options.
Convert a Java Application to .NET
IKVM.NET includes ikvmc, a utility that converts Java .jar files to .NET .dll libraries and .exe applications. In this section, you'll convert a Java application to a .NET .exe.
Navigate to IKVMROOT\samples\hello and enter the following:
After the command completes, you should find a hello.exe file in the current directory. To execute it:
-
Windows / .NET Framework:
Try running hello.exe. If you get a FileNotFound exception when the .NET runtime attempts to load the referenced IKVM.OpenJDK.ClassLibrary.dll, remember that the .NET Framework expects to find referenced dll's in the application directory or in the Global Assembly Cache. Either install the dll's in the Global Assembly Cache, or copy them to the application directory.
-
Linux / Mono:
Run it using the following command:
mono hello.exe
Develop a .NET Application in Java
In this section, you will learn the steps needed to develop .NET applications in Java.
To begin, open a command window and navigate to IKVMROOT\samples\usenetapi. Take a look at ShowDir.java -- this is a Java application that uses the .NET API to display a list of files in the current directory. Notice the imports at the top -- the package names begin with cli.*. These are not packages in the Java API; rather, they are "pseudo" packages that map to .NET namespaces. For more information on this, see the Developer's Guide.
Step 1: Generate Java stubs
IKVM does not come with a Java compiler, so we will compile ShowDir using a standard Java compiler. Since Java compilers can only compile applications that use Java API's, not .NET API's, we have to fool the Java compiler into believing that there is really a Java package named cli.System.IO. The ikvmstub application helps us do this. It generates Java jar files from .NET dll's. The jar files generated by ikvmstub contain Java classes and interfaces that correspond to .NET classes, but don't contain any real code. They contain just enough to satisfy the Java compiler, and allow it to type check the Java application.
Type the following:
Note: On a Linux Mono installation, you will have to type the full pathname to mscorlib.dll, like this:
After the command completes, you should find a file named mscorlib.jar in the current directory.
Step 2: Compile the Java source code
Now, we'll compile the Java source code. If you're using javac, type the following:
(Substitute jikes for javac if you're using that tool.)
After the command completes, you should find ShowDir.class in the current directory.
Step 3: Generate a .NET executable
Now, we'll convert the Java class file to a .NET application. Type the following:
After the command completes, you should find ShowDir.exe in the current directory. You should be able to execute it successfully. (On Windows .NET, remember to copy the IKVM dll's to the current directory.)