从类路径加载本机库 [英] Load Native Library from Class path
问题描述
我的项目设置遵循标准目录布局(不使用Maven):
I have a project setup that follows the Standard Directory Layout (not using Maven though):
src/main
| java
| resources
| library.dll
原生DLL位于资源文件夹和java文件夹中的源。 resources文件夹是Java类路径的成员。
Native DLLs are located in the resources folder and sources in the java folder. The resources folder is a member of the Java class path.
我现在想加载DLL而不必设置JRE -Djava。 library.path
选项或设置 PATH
变量,这样只需双击即可启动生成的jar文件。
I would now like to load a DLL without having to set the JRE -Djava.library.path
option or setting the PATH
variable so the resulting jar file can be started with a simple double click.
是否可以将资源文件夹添加到库搜索路径,而无需在运行jar文件时进行其他配置?
例如设置类似于清单中的 Class-Path
?
Is it possible to add the resource folder to the library search path without having to do additional configuration when running the jar file?
E.g. with a setting similar to the Class-Path
in the Manifest?
推荐答案
有一个旧的hack仍然可以在今天(1.7.0_55和1.8.0_05)工作,允许你使用System.setProperty()进行
a运行时更新,并使
为JVM注意到变化。通常,您执行以下操作:
There is an old-time hack that still works as of today ( 1.7.0_55 & 1.8.0_05 ) to allow you to do to a runtime update using System.setProperty() and have the JVM notice the change. In general, you do the following:
System.setProperty("java.library.path", yourPath);
Field sysPath = ClassLoader.class.getDeclaredField( "sys_paths" );
sysPath.setAccessible( true );
sysPath.set( null, null );
System.loadLibrary(libraryName);
Google java sys_paths 并查找有关此技术的文章。
Google java sys_paths and look for articles about this technique.
注意处理错误/异常。
根据需要恢复原始路径。
Take care to handle errors/exceptions. Restore original paths as needed.
这篇关于从类路径加载本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!