這是我的問題:我試圖在我的 Java 項目中使用 JNI 作為與 Win32 API 對話的一種方式。我在 Eclipse 運行時收到此錯誤:Exception in thread "main" java.lang.UnsatisfiedLinkError: com.printer.PrinterWinAPI.GetStatus(Ljava/lang/String;)Jat com.printer.PrinterWinAPI.GetStatus(Native Method)at com.printer.PrinterWinAPI.<init>(PrinterWinAPI.java:14)at com.printer.PrinterWinAPI.main(PrinterWinAPI.java:25)但是我的問題真正奇怪的是我可以使用 cmd 成功編譯我的項目:javac PrinterWinAPI.java然后程序運行良好:java PrinterWinAPI據我了解,eclipse 成功找到了我的 .dll 文件,但在文件中找不到 GetStatus() 函數。我嘗試使用 x86 和 x86_64 中的 Visual Studio 以及 x86 和 x86_64 中的 mingw gcc 來編譯它。這是我的文件:實現(xiàn)JNI接口的java文件:PrinterWinAPI.javapackage com.printer;public class PrinterWinAPI { static { System.load("C:\\GetPrinterStatus.dll"); } public Long status; public String name; public PrinterWinAPI(String printerName) { this.name = printerName; this.status = this.GetStatus(this.name); } private native long GetStatus(String str); public static void main(String[] args) { PrinterWinAPI printer = new PrinterWinAPI("PRINTER NAME EXAMPLE"); System.out.println(printer.status); }}PrinterWinAPI.h,使用 javah PrinterWinAPI 生成:/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h> /* Header for class PrinterWinAPI */#ifndef _Included_PrinterWinAPI#define _Included_PrinterWinAPI#ifdef __cplusplus extern "C" {#endif /* * Class: PrinterWinAPI * Method: GetStatus * Signature: (Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_PrinterWinAPI_GetStatus (JNIEnv *, jobject, jstring);再一次,程序在命令提示符下使用 javac、javah 和 java 編譯和運行良好,我 99% 確定問題來自 eclipse,但我真的不知道從哪里開始。我花了幾個小時在網上尋找解決方案,但找不到任何東西。為什么eclipse不能運行我的項目,但是手動運行的java二進制文件可以?
1 回答

呼如林
TA貢獻1798條經驗 獲得超3個贊
@user2543253 謝謝你成功了!它是正確編譯的,因為我的 java 文件不在包中,所以頭文件是正確的,但由于 java 文件嵌套在我項目的包中,所以正確的頭文件Java_com_printer_PrinterWinAPI_GetStatus
不是Java_PrinterWinAPI_GetStatus
.
添加回答
舉報
0/150
提交
取消