C # implementations call methods in Java classes

Source: Internet
Author: User

Basic ideas:

A method in a class called Java is implemented in C #, with a focus on packaging a Java-written program into a jar and then using the Open Source Tool IKVM to convert it into a DLL control, called in the. NET environment.

Divided into the following steps:

1. Download JDK6 (Note: JDK7 may not be supported, it is recommended to use JDK6 and Eclipse), install it, and then configure the environment variable path to install the JDK paths (for example: D:\Program files\java\jdk1.6.0_10\bin) After adding to the PATH variable:

Open the DOS box with CMD, enter Javac to see if the configuration is successful, and the configuration will be displayed with more detailed information.

2. Open eclipse to write the Java application and package it into a jar package (you can also use commands under CMD)

After compiling the Hello.java file, form the Hello.class file, then select the Hello.java file and then right click to select Export (export) and then select Jar file and click Next to complete the generated jar files.

You can also manually generate a jar file using the cmd command line:

Where Test_java\bin\com\hello.class is the path to the generated. class file, Com.Hello.jar is the name of the jar file that you want to generate.

3. Download the IKVM required components to the IKVM official website http://www.ikvm.net/

Ikvm-0.42.0.3.zipikvmbin-0.42.0.3.zipopenjdk6-b16-stripped.zip three compressed files and then unzip the Ikvm-0.42.0.3.zip to add the path of the extracted files to the environment variable path, similar to Practices when configuring the JDK.

4. Turn the converted jar package into a DLL control through the IKVM tool.

Use the command ikvmc-out:hello.dll com under cmd. Hello.jar can be converted

5. Create a new C # project and locate the following 3 DLL controls IKVM.OpenJDK.Core.dll, IKVM, under the bin directory of the Ikvm-0.42.0.3.zip extracted file path. Runtime.dll, IKVM. Runtime.JNI.dll add their references to the C # project. Then add your own generated Hello.dll control.

[CSharp]View Plaincopy
  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.ComponentModel;
  4. Using System.Data;
  5. Using System.Drawing;
  6. Using System.Linq;
  7. Using System.Text;
  8. Using System.Windows.Forms;
  9. Introduce a namespace (the name of the package in Java will be converted to namespace by the C # side)
  10. using COM;
  11. Namespace Windowsformshello
  12. {
  13. Public partial class Form1:form
  14. {
  15. Public Form1 ()
  16. {
  17. InitializeComponent ();
  18. }
  19. private void Btnclick_click (object sender, EventArgs e)
  20. {
  21. classes in the//java
  22. Hello hello = new Hello ();
  23. MessageBox.Show (hello. SayHello ());
  24. }
  25. }
  26. }

Clicking on the button on the interface will output the Hello,java information.

C # implementations call methods in Java classes

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.