我的主计算机上运行的JNI应用程序是Windows 7 64位,但它无法在我的笔记本电脑上运行,这是Windows XP 32位。在笔记本电脑上,32位版本的本机DLL加载时没有UnsatisfiedLinkError
,但是在调用实际的本机方法时出现错误。以下是我使用MinGW编译DLL的方法:
cd bin
javah MyClass
move /Y MyClass.h ../
"%mingw64%\bin\g++" -shared -I"C:\Program Files\Java\jdk1.8.0_05\include" -I"C:\Program Files\Java\jdk1.8.0_05\include\win32" -o src/lib/MyLibrary.dll MyClass.cpp
@echo "Making the 32-bit library..."
g++ -shared -I"C:\Program Files\Java\jdk1.8.0_05\include" -I"C:\Program Files\Java\jdk1.8.0_05\include\win32" -o src/lib/MyLibrary_32.dll MyClass.cpp
这是我用来加载库的代码,技术上适用于两个处理器。
String processor = System.getProperty("sun.arch.data.model").equals("32") ? new String("_32") : new String("");
try { System.load(System.getProperty("user.dir") + "\\MyLibrary" + processor + ".dll"); }
catch(UnsatisfiedLinkError e) {
//If the dll was not found, extract the one packaged as a resource in the .jar
InputStream in = ML_Overworld.class.getClassLoader().getResourceAsStream("lib/MyLibrary" + processor + ".dll");
if(in == null) {
printError(String.format("MyLibrary%s.dll was not contained in the jar.", processor));
return;
}
File outPath = new File(System.getProperty("user.dir") + "\\MyLibrary" + processor + ".dll");
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(outPath));
try {
int input;
while((input = in.read()) != -1)
out.write(input);
out.close();
in.close();
}
catch(IOException e2) {
printError(String.format("MyLibrary%s.dll failed to extract: ", processor) + e2.getMessage() + ".");
return;
}
}
catch(FileNotFoundException e3) {
printError(String.format("MyLibrary%s.dll failed to extract: ", processor) + e3.getMessage() + ".");
return;
}
try { System.load(System.getProperty("user.dir") + "\\MyLibrary" + processor + ".dll"); }
catch(UnsatisfiedLinkError e4) {
printError(String.format("MyLibrary%s.dll failed to load: ", processor) + e4.getMessage() + ".");
return;
}
}
作为参考,这是在32位笔记本电脑上抛出Unsatisfied Link Error
的本机方法:
public static native void readMap(String path, cMap m);
[solution]:在编译32位DLL时将--kill-at
添加到g ++参数中。