With maven
I'm building an application which has to load a driver dynamically. With the following code it only works if the driver.so
is positioned inside the resulting JAR file. What can I do that the file can be found outside of the JAR within the path ./natives/driver.so
.
使用maven我正在构建一个必须动态加载驱动程序的应用程序。使用以下代码,仅当driver.so位于生成的JAR文件中时才有效。我该怎么办才能在路径./natives/driver.so中找到JAR之外的文件。
package com.myproject;
public class Starter {
public static void main(String[] args) {
File classpathRoot = new File(Starter.class.getClassLoader().getResource("driver.so").getPath());
System.out.println(classpathRoot);
}
}
Output when driver is positioned inside JAR is:
驱动程序位于JAR内部时的输出是:
jar:file:/home/ted/java/myproject/target/myproject-0.1-SNAPSHOT.jar!/libgdx64.so
Output when positioned outside JAR (in target
as well as in target/natives
directory) is:
位于JAR外部(目标以及目标/本地目录中)的输出是:
null
I start the application via:
我通过以下方式启动申请:
cd /home/ted/java/myproject/target/
java -Djava.library.path=./natives -cp myproject-0.1-SNAPSHOT.jar com.myproject.Starter
What can I do?
我能做什么?
1 个解决方案
#1
Try this:
package com.myproject;
public class Starter {
public static void main(String[] args) {
File file = new File("natives/driver.so");
System.out.println(file);
}
}
or this:
package com.myproject;
public class Starter {
public static void main(String[] args) {
File file = new File(System.getProperty("java.library.path"), "driver.so");
System.out.println(file);
}
}
#1
Try this:
package com.myproject;
public class Starter {
public static void main(String[] args) {
File file = new File("natives/driver.so");
System.out.println(file);
}
}
or this:
package com.myproject;
public class Starter {
public static void main(String[] args) {
File file = new File(System.getProperty("java.library.path"), "driver.so");
System.out.println(file);
}
}