C # Call the java class and jar package methods
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!
Eclipse generates a jar package
First, export the jar package of the common class. The common class refers to the class that contains the main method and does not use other jar packages.
1. In eclipse, select the class or package you want to Export, right-click and select the Export sub-option;
2. In the pop-up dialog box, select java file --- Select JAR file and click next;
3. select the location and name of the JAR package you want to generate in the text box behind the jar file. Note that you should check the Export generated class files and resources and Export java source files and resources, click next;
4. Click the next button twice to reach the JAR Manifest Specification. Note: select the entry class of your jar package in the text box next to Main class at the bottom. Click Finish.
In the dos environment, you can enter the directory where your jar is located and run java-jar name. jar to check whether the operation is correct.
Second, other jar packages are used in the class you want to export. For example, the class you write connects to the database and uses the database driver package CMDL. jar ..
1. First, export the class you want to export to form a jar package, such as test. jar.
2. Create a folder main, for example, under the root directory of drive D;
3. Copy test. jar and CMDL. jar to the main file, right-click test. jar, and decompress it to the current folder. Cut META-INF \ MANIFEST. MF to another place (such as desktop !) ;
4. Right-click CMDL. jar and decompress it to the current folder.
5. In the dos environment, go to the main folder of the d disk and execute jar cvfm new. jar meta-inf/manifest. mf.. Do not forget the last point.
6. Use the compression tool to open your new. jar and overwrite the original new. jar with the META-INF \ MANIFEST. MF you put on the desktop.
In the dos environment, you can enter the directory where your jar is located and run java-jar name. jar to check whether the operation is correct.
First, export the jar package of the common class. The common class refers to the class that contains the main method and does not use other jar packages.
1. In eclipse, select the class or package you want to Export, right-click and select the Export sub-option;
2. In the pop-up dialog box, select java file --- Select JAR file and click next;
3. select the location and name of the JAR package you want to generate in the text box behind the jar file. Note that you should check the Export generated class files and resources and Export java source files and resources, click next;
4. Click the next button twice to reach the JAR Manifest Specification. Note: select the entry class of your jar package in the text box next to Main class at the bottom. Click Finish.
In the dos environment, you can enter the directory where your jar is located and run java-jar name. jar to check whether the operation is correct.
Second, other jar packages are used in the class you want to export. For example, the class you write connects to the database and uses the database driver package CMDL. jar ..
1. First, export the class you want to export to form a jar package, such as test. jar.
2. Create a folder main, for example, under the root directory of drive D;
3. Copy test. jar and CMDL. jar to the main file, right-click test. jar, and decompress it to the current folder. Cut META-INF \ MANIFEST. MF to another place (such as desktop !) ;
4. Right-click CMDL. jar and decompress it to the current folder.
5. In the dos environment, go to the main folder of the d disk and execute jar cvfm new. jar meta-inf/manifest. mf.. Do not forget the last point.
6. Use the compression tool to open your new. jar and overwrite the original new. jar with the META-INF \ MANIFEST. MF you put on the desktop.
In the dos environment, you can enter the directory where your jar is located and run java-jar name. jar to check whether the operation is correct.