Getting Started with Java programs running on the. NET Platform-ikvm.net

Source: Internet
Author: User
Tags command line contains execution mscorlib reference stub linux
Program Summary

IKVM. NET is a MonoAnd the Java implementation of the Microsoft. NET Framework, designed to run Java programs on the. NET platform. This article will give a more detailed introduction to the rationale of the tool, how to get started with Java applications. NET application and how to develop it in Java. NET application), I hope to bring you surprise.

First, introduce

IKVM. NET is a MonoAnd the Java implementation of the Microsoft. NET Framework, designed to run Java programs on the. NET platform. It contains the following builds:
* One for use. NET implementation of Java Virtual machine
* A Java class library. NET implementation
* Dedicated in Java and. NET interaction between the tools

Second, IKVM. NET's components

IKVM. NET contains the following sections:
* IKVM. Runtime.dll:VM run time and all support code. It includes the following features:
Byte Code JIT compiler and Validator: compiles Java Byte code into CIL (C intermediate language) using JIT.
Object pattern Mapping Structure: mapping system.object,system.string,system.exception in. NET to Java.lang.Object in Java code, Java.lang.string,java.lang.throwable.
Manage. NET implementations of local methods (in Classpath).
* IKVM. Gnu. Classpath.dll: The compiled GNU Classpath version, which consists of a Java class library implemented by the Free Software Foundation and some ikvm.net additional code. Note: The GNU classpath here is not part of Ikvm.net, but the former is used in IK.VM.NET.
* IKVM. Jni. [mono| Clr-win32].dll: Manages the C + + assembler by implementing the JNI interface. As an optional part, it is used only when the program is using its own local library. It's not going to be used for pure Java programs.
* Ikvm.exe: Launch execution Program (dynamic mode) very similar to Java.exe.
* Ikvmc.exe: Static compilers that are used to compile Java classes and jars to make them. NET assembly (static mode).
* Ikvmstub.exe: one from. NET assembly to generate a stub class, just like JAVAP. NET assembly. IKVM. NET learn how to stub and replace references to stubs with actual. NET type references.
* IKVM. Awt. WinForms.dll: Very limited, fragmented AWT implementations.

Ii. Status of the project

This project is currently being developed to maximize compatibility with JDK1.4, but there are still some vulnerabilities (especially in the Classpth API).
* AWT and swing are not functional yet.
* Security is a major omission from the IKVM platform. This problem will depend on. NET platform is resolved with the old, but powerful, security model. Despite this, the project now has the ability to successfully run a large Java project.

Three, IKVM principle

1. How to replace the JVM

The IKVM application includes a Java virtual machine that uses. NET implementations. On some occasions, we can use it to replace Java. For example: Java-jar Myapp.jar will be replaced with Ikvm-jar Myapp.jar.

2. Using Java class libraries in. NET Applications

IKVM. NET contains Ikvmc, this in Java bytecode with. NET intermediate Language Converter. If we use one that is used in. NET platform, run Ikvmc–target:library Mylib.jar (Mylib.jar here refers to our jar file) to generate Mylib.dll. For example, the Apache FOP project is an Open-source XSL-FO processor project that is written in the Java language to generate PDF documents from XML. Using ikvm.net technology, we can use the Apache FOP in any. NET application. This is in development. NET application, the IKVM can use the Java Open Source project in this free software warehouse. Although the Java compiler used in. NET is not available in ikvm.net, we can compile the Java source code into JVM bytecode using the open source Jikes compiler, and then use the Ikvmc–target:exe Myapp.jar to produce. NET execution files. We can even use the. NET APIs in our Java code in a way that includes ikvmstub applications.

Iv. Introduction to the use of IKVM

1. System Preparation

Windows platform: Microsoft. NET Framework 1.1 SDK
Windows or Linux platform: Mono Framework 1.0

2. Start installation

The installation process is the same on Windows and Linux platforms, and the files are uncompressed after downloading the binary release version. Open the command or Shell window, the CD enters the Ikvm\bin directory, and executes the IKVM. If we do the right thing, we'll see the following output:

USAGE:IKVM [-options] <class> [args ...] (to execute a class) or Ikvm-jar [-options] <jarfile> [args ...] (To execute a jar file) ...

For ease of use, we can add the Ikvm\bin directory to the system path. Now we're going to use IKVM, just like using a JVM, without configuration. If we need to be in. NET or mono environment use IKVM, please read the following text description carefully:

* Download the Jikes compiler first. If we plan to develop code to run in Java. NET environment, we will have a Java compiler. IKVM. NET does not provide this compiler, so we need compilers capable of generating standard Java class files. Jlikes is a good choice, it is an excellent open source project and applied on a variety of platforms. Of course, the JDK provided by Sun is also very good.

* Install the IKVM DLL in the Windows global assembly buffer. Running based on IKVM DLLs in Windows. NET application,. NET Framework must locate these DLLs. The system first looks in the global assembly buffer and then looks in the current directory. If we want to install these DLL files in the current directory, we will install them in the global assembly buffer: access the. NET Framework configuration in Windows Control Panel and add an assembly buffer. We should at least install IKVM.GNU.Classpath.dll and IKVM.Runtime.dll.

3. Setting up the environment

Before we start writing the code, we need to prepare our environment and add the following path to the system PATH environment variable:
* Contains a directory of IKVM execution files.
* Contains a directory of the C # compiler (CSC in Windows/mono) that is typically C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322 in WINDOWS.
* A directory containing a Java compiler (JAVAC or Jikes).

4. Dynamically executing Java applications

IKVM. NET includes a Java virtual machine implemented by C #. We can start with an example--go into the Ikvmroot\samples\hello directory and compile the sample program:

Javac Hello jar CFM Hello.jar MANIFEST.MF hello.class

Now, after compiling the Hello class with Javac, we use IKVM to run this class:



This command will start IKVM,IKVM to find the file named Hello.class. If found, it is loaded and dynamically executed bytecode. At this point the Hello program will ask us to enter a name, and then we will see a greeting message.

If a problem occurs in the process above, please check the following:
* Check the command line: IKVM as Java needs us to enter the correct class name.
* If the IKVM report classnotfoundexception, check to see if the CLASSPATH environment variable is set. If it is set, clear classpath or add the current directory to the Classpath so that IKVM can find the class in the current directory.

If you run the jar file, we can type:



Tip: Detailed command-line options are available for reference IKVM Handbook

5. Converts a Java program to a. NET Program

IKVM. NET contains IKVMC, a tool that can convert jar files to. NET DLL libraries and EXE applications. Here we'll learn how to convert Java applications to one. NET execution file:

Enter the Ikvmroot\samples\hello directory by entering the following command:

Ikvmc Hello.jar

Note: When we use mono, we need to tell ikvmc how to find the GNU Classpath DLL, for example:

IKVMC-REFERENCE:/USR/LIB/IKVM. Gnu. Classpath.dll Hello.jar

After the command has been executed, we will find that Hello.exe has been generated in the current directory:
* in windows/. NET environment, if we get the exception of FileNotFound, please remember to check. NET Framework wants to look for DLL files in the current directory or in the global assembly buffer. We can install the DLL into the global assembly buffer, or copy the DLL file directly to the current directory, using the method described above.

* In the Linux/mono environment, we use the following command to execute the hello.exe:

Mono Hello.exe

6. Developed in Java. NET application

First go into the Ikvmroot\samples\usenetapi directory and find the Showdir.java file, which uses the. NET API to display a list of files in the current directory. To open this file, we will find that its imported package name begins with the CLI, and these are not Java API packages, they are mapped to. The "pseudo" package for the net namespace. Need to see more information please see IKVM's Developer Handbook

First step: Generate Java stub files

IKVM does not provide a Java compiler, so we can use the standard Java compiler. Because the Java compiler can only compile applications that use the Java API, instead of using the. NET API application. So here we need to "fool" the Java compiler into believing that the name is CLI. System.IO's Package is a real Java package. It is the Ikvmstub program that helps us to finish the work. It generates a jar file from the. NET DLL, and the generated jar file contains the associated. NET class, but does not contain real code and contains only some mapping information. This is done by checking and compiling the Java compiler:

Ikvmstub mscorlib.dll

Note: In the Linux mono environment, we must enter the full path of the DLL file, for example:
Ikvmstub/usr/lib/mscorlib.dll

After the compilation is complete, we will find a file named Mscorlib.jar in the current directory.

Step two: compiling Java source code

Now we will compile the Java source code, if you use Javac, you can enter the following command:

Javac-classpath Mscorlib.jar Showdir.java

After the command completes, the Showdir.class file appears under the current directory.

Step three: Build. NET execution file

Finally, we will convert the Java class file to. NET application, as I said earlier:

Ikvmc Showdir.class

Note: In the Linux mono environment, we need to use the-reference option mentioned earlier:

IKVMC-REFERENCE:/USR/LIB/IKVM. Gnu. Classpath.dll Showdir.class

Then we can see that ShowDir.exe appears in the current directory.

V. Reference resources:
http://www.ikvm.net/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.