After mvn install to generate jar. Can not Loader.loadNativeLibraries() on windows.
The version of ortools is 9.0.9048. I used it on windows. I added the following two jar to the project and I added them as the link said.
Then the two jar is in here of the IDEA.
The pom file is the following:
Then I can run the program normally in IDEA. But when I mvn install to generate the jar file and run the jar by ‘java -jar jarfile.jar’, it errors as:
It said java.nio.file.NoSuchFileException: /BOOT-INF/lib/ortools-win32-9.0.jar!/win32-x86-64/, but when I open the jar in winrar, it exists.
Does anyone know the reason?
Advertisement
Answer
An example of Mac version.
You need two jars when using ortools in Java actually, ortools-java-9.0.9048.jar
and ortools-darwin-x86-64-9.0.9048.jar
. The two jar is unzipped from the official file and they are in the main directory.
ortools-java-9.0.9048.jar
is the algorithm package which you do not need to care to much. Adding dependency to your program is all of you need to do.
The key is the ortools-darwin-x86-64-9.0.9048.jar
. The following code is to read this jar to finally call the algorithm in ortools-java-9.0.9048.jar
:
import com.google.ortools.Loader; Loader.loadNativeLibraries();
It usually works well in IDEA. But when you package the code to a jar file, error happens because of Loader.loadNativeLibraries();
can not find the file in the ortools-darwin-x86-64-9.0.9048.jar
.
The solution is to unzip ortools-darwin-x86-64-9.0.9048.jar
and get the absolute path of libjniortools.dylib
(if you are using linux, it will be a file similar as libjniortools.so
and a file similar as libjniortools.dll
in Windows). And using the following code instead of Loader.loadNativeLibraries();
System.load("Absolute path/libjniortools.dylib");
It will work after you package your code by this method.