Turn Java code into a C # usable DLL

Source: Internet
Author: User

1, first you need to IKVM official website download IKVM. http://sourceforge.net/projects/ikvm/files/, it is important to note that if you are using. NET framework2.0, then the latest version of IKVM is not available, you can use the 0.38.0.2 version of

Configure Environment variables: Right click on "Computer", select "Properties", go to "Advanced system Settings", "Advanced" and "Environment variables", add ";D: \program Files\ikvmbin-7.2.4630.5\ikvm-7.2.4630.5\bin", where the first one"; "optional, followed by" D:\Program files\ikvmbin-7.2.4630.5\ikvm-7.2.4630.5 "for your IKVM path. Test: Through "CMD", enter "Ikvmc", enter the following can see a lot of information. 2, next you want to write a simple Java program, the most popular example of online:Package Com.zht;//Java class to invokePublic class Test {//Java method to invokePublic String returnstring () {return "Hello, zht!";    }}3, you want to package the Java program into a JAR file select the project, right-click, select "O (export/export)", select "Java"-"Jar file", after the next, select the class file (or package) you want to package, select "Export generated class files and resources", and then Next, OK.





4. After you use IKVM to compile the jar file into DLL file cmd, enter "Ikvmc-out:e:\hello.dll E:\hello.jar", Ikvmc is the command,-out: The DLL file followed by the output, followed by a space after the jar file. 5. Call DLL file in C # to create a new C # project (either a console program or a WinForm program), add a reference: 1) hello.dll (DLL file you just generated) 2) IKVM.OpenJDK.Core.dll; IKVM. Runtime.dll; IKVM. Runtime.JNI.dll (these three are basic DLL files, other DLL files are added as appropriate)

Import the namespace through the using COM.ZHT. You can create a new test class, and use the methods of that classreturnstring ();The code is as follows:using System;using System.Collections.Generic;using System.Linq;using System.Text;using Com.zht;namespace Dlltest{Class Program    {static void Main (string[] args)        {Console.WriteLine (New Test ()). Returnstring ());Console.readkey ();        }    }}Running results such as:

Turn Java code into a C # usable DLL

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.