Java and JNI - Loading dll libraries only works on my computer
I'm facing some annoying problems to get my java application working with external c++ dlls, through JNI. The libraries are located inside packages. I copy them to a temporary folder when I need to load them into virtual machine. The dlls are the next:
libcurld.dll; libfftw3f-3.dll; libmad.dll; libsamplerate.dll; main.dll;
The main.dll is the one that implements the native method declared on the java side. This dll depends on the above to run properly. I only compiled the main.ddl on visual studio, one binary for 7 other for xp. The others were downloaded and simply linked. I run the next method to load the libraries on java:
public static boolean loadBinaries(){
String os = System.getProperty("os.name").toLowerCase();
ArrayList<String> bins = new ArrayList<String>();
if(os.indexOf("windows 7") >= 0){
bins.add("/nm/metadata/bin/win/libcurld.dll");
bins.add("/nm/metadata/bin/win/libfftw3f-3.dll");
bins.add("/nm/metadata/bin/win/libmad.dll");
bins.add("/nm/metadata/bin/win/libsamplerate.dll");
bins.add("/nm/metadata/bin/win/seven/main.dll");
}
else if(os.indexOf("windows xp") >= 0){
bins.add("/nm/metadata/bin/win/libcurld.dll");
bins.add("/nm/metadata/bin/win/libfftw3f-3.dll");
bins.add("/nm/metadata/bin/win/libmad.dll");
bins.add("/nm/metadata/bin/win/libsamplerate.dll");
bins.add("/nm/metadata/bin/win/xp/main.dll");
}
File f = null;
for(String bin : bins){
InputStream in = FileManager.class.getResourceAsStream(bin);
byte[] buffer = new byte[1024];
int read = -1;
try {
String[] temp = bin.split("/");
f = new File(TEMP_FOLDER + "/" + temp[temp.length-1]);
File realF = new File(f.getAbsolutePath());
if(realF.exists())
f.delete();
FileOutputStream fos = new FileOutputStream(realF);
while((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
in.close();
System.load(f.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
The executable jar works always perfectly on my machine, but not in others... I got the next errors from my tests:
(xp)
Exception in thread "main" java.lang.UnsatisfiedLinkError: C:\temp\main.dll: Can't find dependent libraries at java.lang.ClassLoader$NativeLibrary.load(Native Method)...
(seven)
C:\temp\main.dll: The application has failed to start because its side-by-side con开发者_JAVA技巧figuration is incorrect...
All the dlls are well written in the temp folder and, as I said, this works well in my computer. I had thought this could be because compiling in debug mode on VS. Unfortunately, turning it to release didn't change anything, unless returning smaller binaries.
What can this be? Is some configuration detail missing on visual studio? Thanks in advance.
First, I would copy all DLLs and then load them in two loops.
Second, from the error message it seems that there is yet another DLL that main.dll requires that is not present on the other machines. It might be the C++ runtime libraries what often not installed in the version you need, which is why many games or other apps install those first.
精彩评论