Java jar to C# dll

簡單的操作讓你能在C#裡面使用java 的lib,C#.NET 與 Java 是兩種不一樣的程式語言,這兩種程式語言都屬於物件導向程式,寫法有許多相似之處,最近有一個案子需要把Java的lib轉C#,但是C#的nuget能下載的資源沒那麼多,只能走將jar轉dll呼叫的方式

安裝 JDK

如果你還沒有 Java 環境的話,需要先下載 JDK 安裝。
可至官方網站

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

接著設定環境變數,在環境變數裡面設定 JAVA_HOME 的 JDK 路徑。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

然後在 Path 裡面加入新環境變數 %JAVA_HOME%\bin。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

測試 JDK 安裝是否完成,可以在 cmd 裡面輸入 java –version 確認是否安裝成功。
javac指令需要重新開機

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

測試 java 檔

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.io.PrintWriter;
 
public class JavaLibraryTest{
	public String getDate(){
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateStr = (df.format(date)).toString();
		return dateStr;
	}
	
	public int[] getArray(){
		int[] arr = new int[] {58, 1, 50, 23, 55, 78, 19, 3, 9, 29};
		return arr;
	}
	
	public HashMap getMap(){
		HashMap map = new HashMap();
		map.put("A", "111");
		map.put("B", "222");
		return map;
	}
	
	public boolean writeFile(){
		try{
			PrintWriter writer = new PrintWriter("C:\\temp\\test.txt", "UTF-8");
			writer.println("The first line");
			writer.println("The second line");
			writer.close();
			return true;
		} catch (Exception e){
			System.out.println("An error occurred.");
			e.printStackTrace();
			return false;
		}
	}
}

編譯JAVA

我使用intellji編譯,設定artifact後產生class file

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

如果沒有要將class合併至jar,可以直接編成jar,
選擇from modules with dependency

合併jar

到目標資料夾,輸入指令

jar uf <target_jar> <target_class>

完成後會合併至target_jar

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

IKVMC (轉dll)

接著會實作將剛剛的 JavaLibraryTest.jar 轉成 .NET 可以讀取的 .dll 檔。
這裡會使用到 IKVM.NET 這個元件。

IKVM.NET 是讓 .NET C# 程式碼可以呼叫 Java 類別庫的一種實現方式。

可以在官網下載主程式,但不知道什麼原因,官網無法開啟網站了。
所以這邊我提供我手上從其他位置下載過的轉碼主程式,我這邊可以提供 2 種版本:

ikvmbin-8.1.5717.0.zip
此版本支援 JDK 1.8 的版本。

ikvmbin-7.2.4630.5.zip
此版本支援 JDK 1.7 的版本。

已經實驗過若用jdk16編譯jar,在用上面2個轉dll,可能會出錯

你可以依照你使用的 JDK 版本來下載對應的主程式。
以下的示範是使用 ikvmbin-8.1.5717.0 這個版本的程式。
下載解壓縮後放在 C:\Temp\ikvm-8.1.5717.0。(可以自行決定位置,只要指令指對路徑即可。)

接著把剛剛產生的 JavaLibraryTest.jar 放到 bin 目錄下,

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

然後在 cmd 輸入以下指令:

C:\Temp\ikvm-8.1.5717.0\bin
ikvmc -target:library JavaLibraryTest.jar

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

在執行 .jar 轉 .dll 的過程中,可能會出現很多的錯誤,通常都是因為參照來源的問題,在 java 如果引用愈多的 library,就會有很多的 warning 出現,IKVM 會試著找各種對應的 library,但也會有找不到的問題,當找不到的話,實際上呼叫 .dll 就容易發生錯誤。

執行後會在 bin 目錄下多一個 JavaLibraryTest.dll 的檔案,這就是要給 .NET 呼叫的檔案。

轉換缺失

使用到越多lib就越有可能發生,如果缺失的是重要的,要自己補回來

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

org.apache.commons.logging 是讀入class的基本lib,

需要先轉dll

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

再用參考補上

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

C#.NET 引用呼叫 Java 類別

接著我們就回到 Visual Studio 2022 的環境下執行。
我這裡用簡單的 ConsoleApp 示範,用 ASP.NET MVC 或 WinForm 程式都可以,但是要注意只能在 .NET Framework 下執行。


VS 安裝 IKVM.NET 套件內容

執行專案的「參考 > 管理 NuGet 套件」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

搜尋 ” IKVM” 可以找到套件,這裡要安裝 IKVM 的版本,選擇版本要跟剛剛把 .jar 轉 .dll 的版本相同,我示範用的是 ikvm-8.1.5717.0,所以在 NuGet 裡面也要選 8.1.5717 版本安裝。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

載入 .dll 類別庫

把剛剛用 .jar 檔轉好的 JavaLibraryTest.dll 檔案,複製到專案的目錄底下。

執行專案的「參考 > 加入參考」。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

再選「瀏覽」,把剛剛的 JavaLibraryTest.dll 檔案載入專案裡面。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

載入後會看到參考類別已經加入這個 .dll。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

對參考類別點兩下後,可以看到裡面提供的方法,跟我們在 Java 建立的方法一樣。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

最後

因為引用了 IKVM.NET 變為專案也認識了 Java 類別庫,所以在 HashMap 可以 using java.util;

簡單的案例如下:

執行後就會出現從 Java 方法來的結果了。

tags: Java C# 'dll'