IKVM. NET Getting Started (1)

Source: Internet
Author: User
Tags mscorlib java open source projects

IKVM. NET is a Java implementation for the mono and Microsoft. NET Framework, designed to run Java programs on the. NET platform. This article will be a more detailed introduction to the principle of this tool, the use of getting started (how to convert Java applications to. NET application, how to develop in Java. NET applications), hoping to bring surprises to everyone.

First, Introduction

IKVM. NET is a Java implementation for the mono and Microsoft. NET Framework, designed to run Java programs on the. NET platform. It contains the following build-up:
* One use. NET implementation of Java virtual machines
* A Java class library. NET implementation
* Committed in Java and. NET interactive tools for

Second, IKVM. NET's components

IKVM. NET contains the following sections:
* IKVM. Runtime.dll:VM Runtime and all support code. It includes the following features:
byte code JIT compiler and validator: use JIT to compile Java Byte code into CIL (C intermediate language).
Object pattern Mapping Structure: Map system.object,system.string,system.exception in. NET to Java.lang.Object in Java code. Java.lang.string,java.lang.throwable.
Manages the. NET re-implementation of the local method (in Classpath).
* IKVM. Gnu. Classpath.dll: The compiled GNU Classpath version, which is made up 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 the ikvm.net, but the former is used in IK.VM.NET.
* IKVM. Jni. [mono| Clr-win32].dll: Manages C + + compilation by implementing a JNI interface. As an optional part, it is only used when the program uses its own local library. It is not used for pure Java programs.
* Ikvm.exe: Startup execution Program (dynamic mode) very similar to Java.exe.
* Ikvmc.exe: Static compilers, which are used to compile Java classes and jars to make them. NET assembly (static mode).
* Ikvmstub.exe: one from. NET assembly to generate stub class tools, as JAVAP as anti-compilation. NET assembly. IKVM. NET knows how to stub and replace the reference to the stub with the actual. NET type reference.
* IKVM. Awt. WinForms.dll: Very limited scattered 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 yet functional.
* Security is a big omission for the IKVM platform. This issue will depend on. NET platform provides an old, but powerful, security model that is addressed. Despite this, the current project has the ability to successfully run large Java projects.

Three, IKVM principle

1. How to replace the JVM

The IKVM application contains a Java virtual machine implemented with. NET. On some occasions, we can replace Java with it. For example: Java-jar Myapp.jar will be replaced with Ikvm-jar Myapp.jar.

2. Using the Java class Library in. NET Applications

IKVM. NET contains Ikvmc, which is in Java bytecode with. NET intermediate-language converters. If we use one that is used in. NET Platform Java Library, run Ikvmc–target:library Mylib.jar (Mylib.jar here to refer 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 for generating PDF documents from XML. Using ikvm.net technology, we can use Apache FOP in any. NET application. This is in development. NET application while using IKVM to use Java Open source projects this free software repository. Although the Java compiler used in. NET is not available in ikvm.net, we can compile the Java source code into the 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 API 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 platforms: Mono Framework 1.0

2. Start installation

The installation process is the same on both Windows and Linux platforms, and after the binary release is downloaded, the files are decompressed. Open command or Shell window, CD into Ikvmin directory, execute 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 Ikvmin directory to the system path. Now we're going to use IKVM just like using a JVM, and we don't need to configure it. If we need to be in. NET or mono environment, please read the following text instructions carefully: IKVM



* First download the Jikes compiler. If we plan to develop code in Java to run on. NET environment, we are going to have a Java compiler. IKVM. NET does not provide this compiler, so we need a compiler that can generate standard Java class files. Jlikes is a good choice, it is an excellent open source project and is applied on a variety of platforms. Of course, Sun provides a good JDK as well.

* Install the IKVM DLL in the global assembly buffer of Windows. You run the IKVM DLL-based in Windows. NET application, the. The NET Framework must locate the locations of 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 need to install them in the global assembly buffer: access the. NET Framework configuration in the Windows Control Panel, and add a compilation buffer. We will install at least IKVM.GNU.Classpath.dll and IKVM.Runtime.dll.

3. Setting up the environment

Before we start writing code, we need to prepare our environment and add the following path to the system PATH environment variable:
* A directory containing IKVM execution files.
* Contains a directory of the C # compiler (CSC in Windows/mono), typically c:windowsmicrosoft.netframeworkv1.1.4322 in Windows.
* Directories containing the Java compiler (JAVAC or Jikes).

4. Executing Java applications dynamically

IKVM. NET includes a Java virtual machine implemented by C #. We can start with an example-go to the Ikvmrootsampleshello 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:

IKVM Hello

This command launches IKVM,IKVM to find a file named Hello.class. If it is found, it is loaded and executed dynamically 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 during the above process, check the following:
* Check the command line: IKVM like Java, we need to enter the correct class name.
* If IKVM reports ClassNotFoundException, check that the CLASSPATH environment variable is set. If set, clear classpath or add the current directory to classpath so that IKVM can find the class in the current directory.

If you run the jar file, we can type:

Ikvm-jar Hello.jar

Tip: For detailed command-line options, refer to the IKVM manual.

5. Converts a Java program to a. NET Program

IKVM. NET contains IKVMC, a tool that can convert jar files to. NET DLL library files and EXE apps. Below we will learn how to convert a Java application to a. NET execution files:

Enter the following command into the Ikvmrootsampleshello directory:

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 is executed, we will find that Hello.exe has been generated in the current directory:
* in windows/. NET environment, if we get the filenotfound exception, please remember to check. NET Framework to look for DLL files in the current directory or in the global assembly buffer. We can either install the DLL into the global assembly buffer or copy the DLL file directly to the current directory, using the method described above.

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

Mono Hello.exe

6. Developed in Java. NET application

First enter the IKVMROOTSAMPLESUSENETAPI directory and locate the Showdir.java file, which uses the. NET API to display a list of files in the current directory. Open this file, we will find its imported package name starts with the CLI, these are not Java API packages, they are mapped to. NET namespace of the "pseudo" package. For more information, see the Developer's Manual for IKVM.

First step: Generate Java stub files

IKVM does not provide a Java compiler, so we can use the standard Java compiler. Since the Java compiler can only compile applications that use the Java API, instead of using the. NET API. So here we need to "fool" the Java compiler into believing that it is named CLI. The System.IO package is a real Java package. It is the Ikvmstub program that helps us to finish the work. It generates a jar file from a. NET DLL, and this generated jar file contains the same as the. NET classes and interfaces, but does not contain real code and contains only some mapping information. This will be checked and compiled by 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 in the current directory.

Step three: Build. NET execution files

Finally we will convert the Java class file to. NET application, as mentioned 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

IKVM. NET Getting Started (1)

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.