1. Package the compiled Class file in java; Package the command JAR
For example, you can package all the class folders in a directory;
Command Used: jar cvf test. jar-C com /.
Here, test. jar is the jar package to be generated; com/. Is the folder under the specified current directory, which includes subfolders and class files;
2. Download the component http://www.ikvm.net required by IKVM from the IKVM official website/
Ikvm-0.42.0.3.zip
Ikvmbin-0.42.0.3.zip
Openjdk6-b16-stripped.zip
3. Set the path
Decompress ikvm-0.42.0.3.zip and add % IKVM_HOME %/bin to path. Here % IKVM_HOME % refers to the main directory of ikvm after decompression.
4. convert a java jar package to A. dll Control
Command: ikvmc-out: IKVM. dll test. jar
Here, IKVM. dll is the name of the. dll Control to be generated, and test. jar is the previously packaged jar package.
5. Add the required controls to the C # project.
1. Create a C #. NET project, first add the required DLLs
% IKVM_HOME %/bin/IKVM. OpenJDK. Core. dll
% IKVM_HOME %/bin/IKVM. Runtime. dll
% IKVM_HOME %/bin/IKVM. Runtime. JNI. dll
2. Add the generated. dll file
Load the previously generated. dll file to the C # project.
Vi. Test
Use the java class in the C # project. The method is the same as that in java. However, the C # syntax using is used for package reference.
Source code:
Java source code:
Package com. zht;
// Java class to be called
Public class Test {
// The Java method to call
Public String returnString (){
Return "Hello, zht! ";
}
}
C # form source code:
Using System;
Using System. Collections. Generic;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Windows. Forms;
Using com. zht;
Namespace KIVMTest
{
Public partial class Form1: Form
{
Public Form1 ()
{
InitializeComponent ();
}
Private void Form1_Load (object sender, EventArgs e)
{
Test t = new Test ();
String str = t. returnString ();
MessageBox. Show (str );
}
}
}
Result:
After the C # window is started, a prompt window is displayed. The content is Hello, zht!
Prepared by Yin Chengliang