C # calls Java class, Jar package method

Source: Internet
Author: User

One, the compiled Java in the class file packaging, Packaging the command jar (or directly using Eclipse packaging is more convenient)

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 #

Source:

Java source code:

 Package Com.zht; // the Java class to invoke  Public class Test {   // Java method to invoke public      String returnstring () {         return "Hello, zht!." ;    }}

C # form Source code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingCom.zht;namespacekivmtest{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); }        Private voidForm1_Load (Objectsender, EventArgs e) {Test T=NewTest (); stringstr =t.returnstring ();        MessageBox.Show (str); }    }}

Results:

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

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.