Using IKVM to tune Java programs in C #

Source: Internet
Author: User
Tags java open source projects

Translated from: http://luanxiyuan.iteye.com/blog/1937081

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,), hope to bring surprises to everyone. If you just want to know how to use it, please skip to step fifth to get started.

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 with. NET implementation of Java virtual machines

Of 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:

The byte code JIT compiler and validator use JIT to compile Java Byte code into CIL (C intermediate language).

The object pattern mapping structure maps 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 was compiled by the 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.

. 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 is similar to Java.exe startup execution Program (dynamic mode).

Ikvmc.exe static compilers, which are used to compile Java classes and jars to make them. NET assembly (static mode).

Ikvmstub.exe a 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 is a very limited scattered AWT implementation.

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–targetlibrary 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–targetexe 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.

Four syntax (Translator Note: This tool is used in Windows command Prompt)

IKVMC [Options] classorjarfile [classorjarfile ...]


See the parameters below.

class file (. class) or package file (. jar)

The name of a Java class or package file can contain wildcard characters (such as *.class).

Parameter---parameter description

-out: Output file----Specifies the file name of the output file. The file name should contain the extension. dll (when the parameter-target is set to the library) or. exe (when the parameter-target is set to EXE or winexe). In general, by default IKVMC will determine the output file name based on the input file name and the-target parameter. However, if you use wildcard characters in the input file name, you must use this option to specify the output file name.

-assembly: assembly name----Specifies the generated assembly name. In general, by default, the assembly name is the output file name.

-target: Target Type----Specifies the type of the target. exe or. dll file to be generated, specifying the following values:

exe--generates an executable program that runs in the Windows Command window.
winexe--generates an. exe application with an interface.
library--generates a dynamic link library (. dll) file.
module--generates a. NET module.
There is no difference between EXE and winexe two parameters on a Linux platform.

-keyfile: Main file name---The primary file name is used to specify the result assembly.

-VERSION:M.M.B.R----Specify the assembly version.

-main: Class name----Specifies the name of the class that contains the main function. By default, if the-target parameter is set to EXE or WINEXE,IKVMC will search for a valid main function, the user will be prompted if found.

-reference:----specifies the class library location. If the Java code uses the. NET programming Interface (API), then use this option to specify these dynamic-link libraries (DLLs). This option can be used multiple times if more than one class library file is referenced. You can use wildcard characters (for example: C:\libs\*.dll).

-recurse:filespec----handles all files in the current folder that can be matched to filespec. For example:-recurse: *.class

-nojni----for non-Java functions (native methods) do not generate Java interfaces (JNI) for non-Java code.

-resource: Path name----Specifies the reference path name of the Java resource.

-exclude: File name--the list of class names to exclude.

-debug----the debug information output. Note: This is useful only if the. class file contains debug information (that is, compiled with the javac-g parameter).

-srcpath: Path----Specifies the location of the source code. Used in conjunction with-debug. The program assigns the search path of the class in the package to this path and searches for the source code of the class from there.

-xtrace: Name----Displays all track points for the specified name.

-xmethodtrace: Function Name----The generated function name specified, which generates the functions tracker (method trace) into this function.


A few notes:

IKVMC generates. NET assemblies using Java class files and package files. It translates the Java two code in the input file into. NET Common Intermediate language (CIL). and use it to generate:

. NET executable file (-target:exe or–target:winexe)
. NET class library (-target:library)
. NET module (-target:module)
Java programs typically consist of a series of package files. IKVMC can convert multiple package files (and class files) into a single. NET executable program or class library file. For example, a program that contains Main.jar, Lib1.jar, and Lib2.jar can be converted into a separate main.exe file.

When working with multiple package files, if there are multiple identical classes or source files, IKVMC uses the first read-in version and ignores the later read. A warning message is generated. Therefore, the order of the package files is critical.

Attention:

For best results, when using IKVMC to convert Java programs, follow the order of the Classpath in the Java program, and type the list of packages to be converted into the IKVMC command line.

Five Getting Started with
    1. to IKVM official website download IKVM Required components 1) ikvm-0.40.0.1.zip; 2) ikvmbin-0.40.0.1.zip; 3) Openjdk6-b12-stripped.zip
    2. Unzip the Ikvm-0.40.0.1.zip and add the%ikvm_home%\bin to the path. The%ikvm_home% here refers to the IKVM home directory after decompression.
    3. Download IKVM.OpenJDK.ClassLibrary.dll to Debian IKVM. Or go to rayfile to download data.tar.gz. This file can be extracted directly with WinRAR.
    4. You need to download a simple tool Undeb to unzip the. deb file. can also be downloaded to Rayfile
    5. Unzip the Ikvm_0.38.0.2+dfsg-2_all.deb file.
    6. Create a new Java file, such as
 Package MyPackage;  Public class Hello {      public  String SayHello () {             = ' Hello ', this string is the from Java program. " ;               return str;      }}

7 Compile the Hello.java file, and then package the mypackage into a jar file, such as Jar CVF Myjava.jar MyPackage

8 Use Ikvmc-out:myjava.dll Myjava.jar. You should now be seeing a MyJava.dll file. You can also specify a result assembly at conversion, such as: Ikvmc-target:library myjava.jar-keyfile:key.snk

9 Create a new C #. NET project, first add the necessary DLLs

1)%IKVM_HOME%\BIN\IKVM. OpenJDK.Core.dll;

2)%IKVM_HOME%\BIN\IKVM. Runtime.dll;

3)%ikvm_home% \BIN\IKVM.    Runtime.JNI.dll; (IKVM. openjdk.classlibrary.dll[This DLL is in the Ikvm_0.38.0.2+dfsg-2_all.deb\usr\lib\cli folder. Or it can be found in the USR\LIB\CLI in data.tar.gz)

Add the 8th step of the generated MyJava.dll to C #. NET project, then the using%package_name% can implement a call to Java.

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingMyPackage;namespacetestikvm{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidForm1_Load (Objectsender, EventArgs e) {                   }        Private voidButton1_Click (Objectsender, EventArgs e) {Hello H1=NewHello (); String S1=H1.sayhello (); Txbx. Text=S1; }    }}

Using IKVM to tune Java programs in C #

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.