C # Call Java code

Source: Internet
Author: User
C # Call the Java code

1. Package the compiled class file in Java; Package the command jar

1. all Java classes must be placed in a package. Do not use the default package, C # cannot find the call between classes (C # references may not reference the content in the default namespace in other DLL, so the Java file must declare the package so that it can be converted into a namespace. In addition, because the default access permission method in Java is converted to the internal method, these methods must be explicitly declared as public in Java, I use eclipse and export the program to a jar file.

In addition, this version does not seem to support the Java GUI. When I debug a new control, C # will say that this method is not implemented.

2. You can also use the command: 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/

Ikvmbin-0.44.0.5.zip

3. Set the path

Decompress ikvmbin-0.44.0.5.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 (add reference in references, and then browse find DLL)

% 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. First use the using package name to find the class

 

Source code:

Java source code:

Package rstar
// 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 rstar;

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!

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.