JNI applications require that you set the java.library.path when they run.
Note: Here JNI refers to the Java Native Interface which is the interface between managed Java code and the "native" operating system.
Let's assume you have compiled the diet.java sample from the Gurobi distribution, and now want to run it as an application. If you copy the libraries 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.
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*
libGurobiJni90.so*
libgurobi90.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:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no GurobiJni95 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.