IKVM 讓 .NET 應用程式能直接載入並執行 Java 類別。本文記錄從下載元件到完成跨語言呼叫的完整步驟。
ikvm-0.40.0.1.zip、ikvmbin-0.40.0.1.zip、openjdk6-b12-stripped.zip。ikvm-0.40.0.1.zip,將 %IKVM_HOME%\bin 加入系統 PATH。IKVM.OpenJDK.ClassLibrary.dll(可從 Debian IKVM 套件 ikvm_0.38.0.2+dfsg-2_all.deb 解壓取得,需 undeb 工具解壓 .deb 檔)。package com.helloikvm.simple;
public class SimpleReturnString {
public String returnString() { return "Hello, IKVM!"; }
public void throwException() throws Exception { throw new Exception("Hello, Exception!"); }
}
編譯並打包成 IKVM.jar。
ikvmc -out:IKVM.dll IKVM.jar
產生 IKVM.dll 供 C# 專案參考。
新增下列 DLL 參考:
%IKVM_HOME%\bin\IKVM.OpenJDK.Core.dll%IKVM_HOME%\bin\IKVM.Runtime.dll%IKVM_HOME%\bin\IKVM.Runtime.JNI.dllIKVM.OpenJDK.ClassLibrary.dll(來自步驟 3)IKVM.dllusing com.helloikvm.simple;
namespace IKVMTest {
public partial class Form1 : Form {
private void btnTest_Click(object sender, EventArgs e) {
SimpleReturnString javaObj = new SimpleReturnString();
string result = javaObj.returnString();
txtTest.Text = result;
}
}
}
using 陳述式對應。catch。