3

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.

enter image description here

Then the two jar is in here of the IDEA.

enter image description here

The pom file is the following:

enter image description here

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:

enter image description here

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.

enter image description here

Does anyone know the reason?

Muz
  • 699
  • 1
  • 11
  • 25

3 Answers3

4

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.

Muz
  • 699
  • 1
  • 11
  • 25
0

The official artifacts are:
groupe: com.google.ortools, artifact: ortools-java

https://search.maven.org/artifact/com.google.ortools/ortools-java/9.0.9048/jar

Mizux
  • 8,222
  • 7
  • 32
  • 48
  • I can not import com.google.ortools if adding the link's dependency to the pom file. – Muz Jun 09 '21 at 08:47
  • well, did you know last maven install plugin 3.0.0-M1 can read the pom.xml inside the archive ? I'm not sure renaming like you did the group and artifact id will work... see https://github.com/or-tools/java_or-tools – Mizux Jun 09 '21 at 09:01
  • It is correct in pom file, meaning that the program can download the given dependency. But the ortools can not import ortools in code. – Muz Jun 09 '21 at 10:19
  • 1
    Solved by using System.load("xxx\\jniortools.dll"), jniortools.dll comes from one of the ortools jars. – Muz Jun 15 '21 at 14:12
-1

For macOS, you can try this code, similar like @Muz solution

public static void loadOrToolLibrary() {
        String os = System.getProperty("os.name", "generic").toLowerCase(Locale.ENGLISH);
        if (os.equals("mac os x")) { // only for MAC local
            File file = new File("src/main/resources/macosLocal/libjniortools.dylib");
            String absolutePath = file.getAbsolutePath();
            System.load(absolutePath);
        } else {
            Loader.loadNativeLibraries();
        }
    }

uncle bob
  • 570
  • 1
  • 10
  • 21