java IntelliJ Idea 和 JNI:确保 DLL 位于需要执行的位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14442641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ Idea and JNI: Making Sure DLL's Are Where They Need to be for Execution
提问by Brendon Dugan
I am working with a third party library in a project, and it includes two .dll files and a .jar fie to provide a JNI Wrapper. The test project that the third party vendor included in the .jar file can be called using the following command:
我在一个项目中使用第三方库,它包括两个 .dll 文件和一个 .jar fie 以提供 JNI 包装器。可以使用以下命令调用第三方供应商包含在 .jar 文件中的测试项目:
java -cp product.jar com.company.samples.product.Test
This works just fine, as long as the .jar and both of the dll's are in the same directory.
只要 .jar 和两个 dll 位于同一目录中,这就可以正常工作。
In my project I have placed the jar file and the dll's in the same directory, and then referenced the jar file as a library in IntelliJ Idea, but this causes the library to be unable to find the dll's. Is there a way that I can tell Idea where the dll's are so it can output them in the same directory as the jar file during execution?
在我的项目中,我将 jar 文件和 dll 放在同一目录中,然后将 jar 文件引用为 IntelliJ Idea 中的库,但这会导致库无法找到 dll。有没有办法告诉 Idea dll 的位置,以便它可以在执行期间将它们输出到与 jar 文件相同的目录中?
回答by Developer Marius ?il?nas
In IntelliJ when you would like to add a directory where .dllis found do:
在 IntelliJ 中,当您想添加一个找到.dll的目录时,请执行以下操作:
1) Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S).
1) 打开项目结构对话框(例如Ctrl+Shift+Alt+S)。
2) In the left-hand pane of the dialog, select Modules.
2) 在对话框的左侧窗格中,选择模块。
3) In the pane to the right, select the module of interest.
3) 在右侧的窗格中,选择感兴趣的模块。
4) In the right-hand part of the dialog, on the Module page, select the Dependencies tab.
4) 在对话框右侧的模块页面上,选择依赖项选项卡。
5) On the Dependencies tab, click "+" and select Jars or directories.
5) 在依赖项选项卡上,单击“+”并选择 Jars 或目录。
6) In the dialog that opens, select the necessaryfiles and folders. These may beindividual .class and .java files, directories and archives (.jar and .zip) containing such files as well as directories with Java native libraries(.dll, .so or .jnilib). Click OK.
6) 在打开的对话框中,选择必要的文件和文件夹。这些可能是单独的 .class 和 .java 文件、包含此类文件的目录和档案(.jar 和 .zip)以及带有 Java 本地库(.dll、.so 或 .jnilib)的目录。单击确定。
See for original explanation on https://www.jetbrains.com/help/idea/2016.3/configuring-module-dependencies-and-libraries.html#d1860048e28.
This is how I add .dll(sql_auth.dll) for JDBC driver to use integrated security of MS SQL.
这就是我为 JDBC 驱动程序添加.dll( sql_auth.dll) 以使用 MS SQL 的集成安全性的方式。
回答by pickypg
You can set the java.library.path
system property to point out to Java where to look for native libraries.
您可以设置java.library.path
系统属性以指示 Java 在哪里查找本机库。
-Djava.library.path=path/to/dll
Note, according to this answer, it is a read-only field that is checked before you have the opportunity to change it in code, but they provide a work-around. In general, I have never needed to set the value at runtime compared to at launch.
请注意,根据此答案,它是一个只读字段,在您有机会在代码中对其进行更改之前会对其进行检查,但它们提供了一种解决方法。一般来说,与启动时相比,我从来不需要在运行时设置值。
回答by Jayan
The DLL are loaded from path variable. You could change the value when launching the application.
DLL 从路径变量加载。您可以在启动应用程序时更改该值。
Alternatively, you can construct the path to dll (again by using environment variable /system variable) and load it via System.load()
或者,您可以构建 dll 的路径(再次使用环境变量/系统变量)并通过System.load()加载它