sup*_*eta 20 java java-native-interface jar loading relative-path
我正在开发一个应该作为jar发布的Java应用程序.该程序依赖于JNI调用的C++外部库.要加载它们,我使用System.load绝对路径的方法,这很好.
但是,我真的想在JAR中"隐藏"它们,所以我创建了一个包来收集它们.这迫使我加载一个相对路径 - 包路径.通过这种方法,我让用户在任何目录中运行JAR,而不用担心链接DLL或无聊以前的安装过程.
这会引发预期的异常:
线程"main"中的异常java.lang.UnsatisfiedLinkError:期望库的绝对路径
我怎样才能使这个工作?
将DLL复制到文件夹(下面解释)的方法仅在我在eclipse环境下运行时才有效.运行导出的JAR,DLL二进制文件很好地创建,但加载JNI会抛出下一个异常:
线程"main"java.lang.reflect.InvocationTargetException中的异常
Run Code Online (Sandbox Code Playgroud)at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:56) Caused by: java.lang.UnsatisfiedLinkError: C:\Users\Supertreta\Desktop\nm files\temp\jniBin.dll: Can't find dependent libraries at java.lang.ClassLoader$NativeLibrary.load(Native Method)
我运行这个加载方法:
public static void loadBinaries(){
String os = System.getProperty("os.name").toLowerCase();
if(os.indexOf("win") >= 0){
ArrayList<String> bins = new ArrayList<String>(){{
add("/nm/metadata/bin/dependence1.dll");
add("/nm/metadata/bin/dependence2.dll");
add("/nm/metadata/bin/dependence3.dll");
add("/nm/metadata/bin/dependence4.dll");
add("/nm/metadata/bin/jniBin.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]);
FileOutputStream fos = new FileOutputStream(f);
while((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.load(f.getAbsolutePath());
}
}
Run Code Online (Sandbox Code Playgroud)
我认为这可能是一个访问权限问题,但不知道如何解决它.你怎么看?
Kur*_*lor 25
我不相信你可以直接从JAR加载DLL.您必须采取中间步骤将DLL复制出JAR.以下代码应该这样做:
public static void loadJarDll(String name) throws IOException {
InputStream in = MyClass.class.getResourceAsStream(name);
byte[] buffer = new byte[1024];
int read = -1;
File temp = File.createTempFile(name, "");
FileOutputStream fos = new FileOutputStream(temp);
while((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
in.close();
System.load(temp.getAbsolutePath());
}
Run Code Online (Sandbox Code Playgroud)