C # calls the Java class

Source: Internet
Author: User

C # calls the Java class(2011-01-07 14:02:05) reproduced
Category: Java Learning

1.

In Eclipse, create a new Java project named Hello, which contains only one file, Hello.java, with the following code:

public class Hello {

public string GetString (String str) throws Exception {
Try
{
Return "Hello! "+ str;
}
catch (Exception ex)
{
Return "error occurred";
}
}

}

2.

The Hello project file is hello.jar,eclipse to provide this functionality.

3.

to IKVM official website download IKVM Required Components http://www.ikvm.net/, I am the version is ikvmbin-0.45.4013.

Decompression Ikvmbin-0.45.4013.zip, for ease of use, the extracted files and folders are placed under C:\ikvm\.

4. Place the Hello.jar under C:\ikvm\. Use IKVM to convert the Hello.jar package to a Hello.dll control. Method: Execute in CMD

C:\ikvm\bin\ikvmc-out:c:\ikvm\hello.dll C:\ikvm\hello.jar

5.

New C # project, adding IKVM DLL references and hello.dll references

BIN\IKVM. OpenJDK.Core.dll

BIN\IKVM. Runtime.dll

BIN\IKVM. Runtime.JNI.dll

Hello.dll

Description: The three DLLs in the above IKVM are the smallest DLLs (because I have only printed a string in Hello.java, no other complex code), if you have more features in your Java class using the JDK, refer to the other DLLs that start with IKVM under bin as needed

6.

Test, the following is C # code

Hello t = new Hello ();
String str = t.getstring ();
MessageBox.Show (str);

7. Not all Java classes can be called successfully, which depends on what features are supported in the JDK, but it is absolutely IKVM.

C # calls the Java class

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.