JNI applications require that you set the java.library.path when they run.
JNI is the "Java Native Interface" -- the interface between managed Java code and the "native" operating system. It enables Java applications to run code in native libraries such as Gurobi. This article describes how to make this work for Java applications that use Gurobi.
Let's assume you have compiled the diet.java sample from the Gurobi distribution, and now you want to run it as an application. It needs to be able to "find" three files:
${GUROBI_HOME}/bin/gurobi120.dll
${GUROBI_HOME}/bin/GurobiJni120.dll
${GUROBI_HOME}/lib/gurobi.jar
If you copy those files into the same folder as Diet.class, then using this Java command will work:
java -classpath=.:./gurobi.jar Diet
If you move the libraries and jar files into a different directory, you will need more settings as shown in the "Path Settings" section.
Path Settings
At runtime, there is a search path for the dynamic libraries required by Gurobi in Java. If you do not set this, you will get an UnsatisfiedLinkError.
- Linux: Set the environment variable LD_LIBRARY_PATH in the system environment.
-
Mac: Set the environment variable DYLD_LIBRARY_PATH.
-
Note: When MacOS is protected by SIP (System Integrity Protect), setting DYLD_LIBRARY_PATH will have no effect. In that case, there are two options:
- Install Gurobi Optimizer in the default location
- Turn off SIP
-
Note: When MacOS is protected by SIP (System Integrity Protect), setting DYLD_LIBRARY_PATH will have no effect. In that case, there are two options:
- Windows: Update the PATH environment variable to include the folder with the DLLs.
Example
For example, assume the current directory looks like this:
Diet.class
gurobi.lic
./lib (subfolder)
gurobi.jar*
libGurobiJni110.so*
libgurobi110.so*
Now you must tell Java how to find the libraries.
Linux
export LD_LIBRARY_PATH=./lib:${LD_LIBRARY_PATH} java -Djava.library.path=./lib -classpath .:./lib Diet
MacOS
export DYLD_LIBRARY_PATH=./lib:${LD_LIBRARY_PATH} java -Djava.library.path=./lib -classpath .:./lib Diet
Windows
SET PATH=./lib:%PATH% java -Djava.library.path=./lib -classpath .:./lib Diet
When the environment variables are not set as described above, you may receive an error similar to this:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no GurobiJni110 in java.library.path: [/usr/java/packages/lib, /usr/lib/x86_64-linux-gnu/jni, /lib/x86_64-linux-gnu, /usr/lib/x86_64-linux-gnu, /usr/lib/jni, /lib, /usr/lib]
at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2670)
at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)
at java.base/java.lang.System.loadLibrary(System.java:1873)
at gurobi.GurobiJni.<clinit>(GurobiJni.java:304)
at gurobi.GRBEnv.<init>(GRBEnv.java:70)
at gurobi.GRBEnv.<init>(GRBEnv.java:46)
at Diet.main(Diet.java:45)
Further information
Comments
0 comments
Article is closed for comments.