C # calls the Java class(2011-01-07 14:02:05)
reproduced
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