C # calls Java class, Jar package method

Source: Internet
Author: User

One, the compiled Java class file packaging; Package Command jar

For example, all the class folders under a directory are packaged and processed;

Command used: Jar CVF test.jar-c com/.

Where Test.jar is the jar package to be generated; com/. To the specified folder in the current directory, which includes subfolders and class files;

Second, to IKVM official website download IKVM Required Components http://www.ikvm.net/

Ikvm-0.42.0.3.zip

Ikvmbin-0.42.0.3.zip

Openjdk6-b16-stripped.zip

Third, set the path

Unzip the Ikvm-0.42.0.3.zip and add the%ikvm_home%/bin to the path. The%ikvm_home% here refers to the IKVM home directory after decompression.

Iv. Converting a Java jar package to a. dll control

Command used: Ikvmc-out:ikvm.dll Test.jar

Where IKVM.dll is the file name of the. dll control that will be generated; Test.jar is the jar package file that was previously packaged.

V. Adding the required controls to a C # project

1. Create a new C #. NET project, first add the necessary 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

To load a previously generated. dll file into a C # project

VI. Testing

Java classes are used in C # projects in the same way as Java. But the reference to the package uses the syntax using C #

Vii. Supplementary notes:

1. If your Java class is complex, you need to add all the. dll library files to the reference without error

2. You can not set the environment variables, directly in the bin directory of the IKVM, the jar is copied over, hold down SHIFT right click, here to open the command line.

3. Only the supplement is my description, the other is reproduced, reproduced please retain the source, thank you!

Source:

Java source code:

Package com.zht;
The Java class to invoke
public class Test {
The Java method to invoke
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);
}
}
}

Results:

After starting the C # window, a prompt window appears with the following: Hello, zht!

article source : http://blog.csdn.net/black0707/article/details/5769366

C # calls Java class, Jar package method

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.