IntelliJ Idea and JNI: Making Sure DLL's Are Where They Need to be for Execution

21,413

Solution 1

In IntelliJ when you would like to add a directory where .dll is found do:

1) Open the Project Structure dialog (e.g. Ctrl+Shift+Alt+S).

2) In the left-hand pane of the dialog, select Modules.

3) In the pane to the right, select the module of interest.

4) In the right-hand part of the dialog, on the Module page, select the Dependencies tab.

5) On the Dependencies tab, click "+" and select Jars or directories.

6) In the dialog that opens, select the necessary files and folders. These may be individual .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.

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.

Solution 2

You can set the java.library.path system property to point out to Java where to look for native libraries.

-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.

Solution 3

The DLL are loaded from path variable. You could change the value when launching the application.

Alternatively, you can construct the path to dll (again by using environment variable /system variable) and load it via System.load()

Share:
21,413
Brendon Dugan
Author by

Brendon Dugan

Updated on March 03, 2020

Comments

  • Brendon Dugan
    Brendon Dugan about 4 years

    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:

    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.

    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?