问题
I have a JNI application working on my main computer which is Windows 7 64-bit, but it fails to work on my laptop which is Windows XP 32-bit. On the laptop, the 32-bit version of the native DLL loads without an UnsatisfiedLinkError
, but I get the error when the actual native method is called. Here is how I compile my DLLs using MinGW:
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
Here is the code I use to load the library, which technically works on both processors.
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;
}
}
And for reference, here is the native method which throws an Unsatisfied Link Error
on the 32-bit laptop:
public static native void readMap(String path, cMap m);
[solution]: added --kill-at
to the g++ arguments when compiling the 32-bit DLL.
来源:https://stackoverflow.com/questions/24398646/java-jni-native-library-loads-on-64bit-and-32bit-but-doesnt-run-on-32bit