IKVM.NET
 
   

Developing .NET Applications in Java

This section discusses information you need to know when you want to develop .NET applications in Java.

Overview

IKVM makes it possible to develop .NET applications using the Java language. Here's how it works:

  1. Identify .NET classes you want to use in your application.
  2. Identify which .NET dll's contain the .NET classes you identified in step 1.

    Tip: If you're developing on Windows, the Microsoft .NET SDK Class Reference documentation identifies the assembly / dll for a .NET class at the bottom of each class overview page.

  3. Use the ikvmstub application to generate a Java jar file for each dll you identified in step 2.

    The ikvmstub tool analyzes the .NET classes in the designated dll and generates a jar file containing Java interfaces and stub classes. This information is needed by the Java source compiler, which knows nothing about .NET assemblies.

  4. Compile your Java source code using javac or jikes, with the ikvmstub-generated jar files on the compiler classpath.
  5. Compile the resulting Java classes using ikvmc. Use the -reference option to reference the dll's containing the .NET classes you used; do not include the ikvmstub-generated jar files on the compiler classpath.

For an example of this, see the tutorial.

Mapping .NET API's to Java

When ikvmstub generates a stub jarfile, it has to prevent namespace conflicts between Java API classes and generated stub classes. It must also map .NET features such as properties, delegates, enumerations, and variable-length argument lists to Java language equivalents.

To prevent namespace conflicts, ikvmstub creates Java package names from .NET namespaces by prefixing them with cli. For example, a .NET class in the System.IO namespace would have a stub generated for it in a Java package named cli.System.IO. So, when writing Java code that uses the System.IO.File class, you would use one of the following import statements in your Java code:

import cli.System.IO.*; import cli.System.IO.File;

The following sections discuss how .NET features are mapped to the Java language. Some of the mappings, such as properties and enumerations, are fairly straightforward. Others, such as delegates and event handling, require a little more work.

Tip: Java development tools that provide code assist features are a great help when writing applications that use .NET API's. If you install the ikvmstub-generated jar files into your favorite Java IDE, you can use code completion to help you use the .NET methods, properties, and enumerations correctly. Note, however, that you will not be able to test your applications using your Java IDE debugger.

Properties

Since Java has no direct language support for properties, ikvmstub maps .NET properties to Java getter and setter methods. A .NET property defined in C# like this:

    public datatype property-name {
        get { ... }
        set { ... }
    }

would be translated to a pair of Java stub methods, like this:

    public datatype get_property-name( ) { ... }
    public void set_property-name(datatype value) { ... }

Here is an example of C# code that uses a property, and how you would access the same property in Java:

C# Java
int weight = bear.Weight; bear.Weight = 15;
int weight = bear.get_Weight(); bear.set_Weight(15);

Enumerations

TODO. For now, see the IKVM Weblog, March 20, 2004 entry.

Delegates and Event Processing

TODO. For now, see the IKVM Weblog, March 20, 2004 entry. Also, see the winforms sample.

Varargs

TODO. For now, see the IKVM Weblog, March 20, 2004 entry. Also, see the usenetapi/CreateFile.java sample.

by Stephen Schaub