Basic ideas:
A method in a class called Java is implemented in C #, with a focus on packaging a Java-written program into a jar and then using the Open Source Tool IKVM to convert it into a DLL control, called in the. NET environment.
Divided into the following steps:
1. Download JDK6 (Note: JDK7 may not be supported, it is recommended to use JDK6 and Eclipse), install it, and then configure the environment variable path to install the JDK paths (for example: D:\Program files\java\jdk1.6.0_10\bin) After adding to the PATH variable:
Open the DOS box with CMD, enter Javac to see if the configuration is successful, and the configuration will be displayed with more detailed information.
2. Open eclipse to write the Java application and package it into a jar package (you can also use commands under CMD)
After compiling the Hello.java file, form the Hello.class file, then select the Hello.java file and then right click to select Export (export) and then select Jar file and click Next to complete the generated jar files.
You can also manually generate a jar file using the cmd command line:
Where Test_java\bin\com\hello.class is the path to the generated. class file, Com.Hello.jar is the name of the jar file that you want to generate.
3. Download the IKVM required components to the IKVM official website http://www.ikvm.net/
Ikvm-0.42.0.3.zipikvmbin-0.42.0.3.zipopenjdk6-b16-stripped.zip three compressed files and then unzip the Ikvm-0.42.0.3.zip to add the path of the extracted files to the environment variable path, similar to Practices when configuring the JDK.
4. Turn the converted jar package into a DLL control through the IKVM tool.
Use the command ikvmc-out:hello.dll com under cmd. Hello.jar can be converted
5. Create a new C # project and locate the following 3 DLL controls IKVM.OpenJDK.Core.dll, IKVM, under the bin directory of the Ikvm-0.42.0.3.zip extracted file path. Runtime.dll, IKVM. Runtime.JNI.dll add their references to the C # project. Then add your own generated Hello.dll control.
[CSharp]View Plaincopy
- 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;
- Introduce a namespace (the name of the package in Java will be converted to namespace by the C # side)
- using COM;
- Namespace Windowsformshello
- {
- Public partial class Form1:form
- {
- Public Form1 ()
- {
- InitializeComponent ();
- }
- private void Btnclick_click (object sender, EventArgs e)
- {
- classes in the//java
- Hello hello = new Hello ();
- MessageBox.Show (hello. SayHello ());
- }
- }
- }
Clicking on the button on the interface will output the Hello,java information.
C # implementations call methods in Java classes