C # Call the java class and jar package methods

Source: Internet
Author: User

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

Related Article

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.