9

I a newbie in java, I wanna try google or-tools for vehicle routing problem

Just try to run java example from here

But I got this exception: java.lang.UnsatisfiedLinkError: no jniortools in java.library.path

There is a line of code which load system lib "jniortools". But I don't know where to get that lib.

I'm using mac osx.

Any ideas?

pham cuong
  • 849
  • 1
  • 12
  • 24

4 Answers4

2

OR-Tools is a C++ library with wrapper in Java using SWIG (which do JNI call etc...). I.e. this is a native library not a "pure" java lib...

So to use ortools in java you must tweak the java.library.path e.g. when using ortools from source and running a program from root_dir:

make third_party
make java
java -Djava.library.path=lib -cp objs:lib/com.google.ortools.jar:lib/protobuf.jar Program

note: ortools depends on protobuf.jar (which is compiled by ortools makefile third_party target rules)

documentation: https://developers.google.com/optimization/introduction/run_programs#running-the-java-example

Mizux
  • 8,222
  • 7
  • 32
  • 48
  • I wish Gradle integration arrives someday. Adding in IntelliJ at VM Options: -Djava.library.path=lib -cp objs:lib/com.google.ortools.jar:lib/protobuf.jar works, but then the Gradle libs don't work. – Georgios Sep 20 '19 at 18:21
  • @mizux `java -Djava.library.path=lib -cp objs:lib/com.google.ortools.jar:lib/protobuf.jar Program` what does this line exactly do? Is there a way to generate a "jniortools.so" file? I am having the same problem but I was attempting to create a Maven dependency for the newest version. https://github.com/panavis/google-or-tools-maven – amucunguzi Aug 28 '20 at 22:21
  • 1
    @amucunguzi you should use the CMake based build which will generate a maven package of ortools for you + add a Loader.java so you won't have to deal with LD_LIBRARY path... – Mizux Aug 29 '20 at 08:00
  • @Mizux, how do I add the Loader.java class? If you can a link or a few steps to follow, that would be appreciated. Thank you. – amucunguzi Aug 29 '20 at 17:57
  • @Mizux finally managed to create a maven dependency and deployed it successfully to the central repository. Thank you. – amucunguzi Sep 03 '20 at 17:21
  • I meet problem when use it with Spark, could you kindly tell me any suggestion for adding this dependency in spark-submit parameter? – linpingta Oct 24 '20 at 08:43
2

We were using Gradle for the build and Linux OS. These are the steps we followed to resolve this issue

  • Add this line of code in the application's main class.

     System.loadLibrary("jniortools");
    
  • Download the OR Tools jar files for linux and keep them in any a folder in your project . ( Here, it is <project_folder>/libs ). And add these lines in your build.gradle file

    compile files('libs/ortools-java-8.1.8487.jar')
    
    compile files('libs/ortools-linux-x86-64-8.1.8487.jar')
    
    compile group: 'net.java.dev.jna', name: 'jna', version: '5.6.0'
    
  • Additionally, we also unzipped the jar ortools-linux-x86-64-8.1.8487.jar in the same folder ( libs )

    jar -xvf ortools-linux-x86-64-8.1.8487.jar
    
  • While running the application ( after building it as a jar file ), we are mentioning the native library path where the .so file will be present

    java -Djava.library.path="/home/user/<project_name>/libs/linux-x86-64/" -jar /home/user/<project_name>/build/libs/<project_name>.jar
    
1

Since release v7.8 (or using the master branch) you can generate maven packages using the CMake based build.

git clone --depth=1 --single-branch -b master git@github.com:google/or-tools.git
cd or-tools
cmake -S. -Bbuild -DBUILD_JAVA=ON
cmake --build build -v

Then you'll have maven packages in build/java/ortools-java and build/java/ortools-<native> (which are two regular maven project...)

Mizux
  • 8,222
  • 7
  • 32
  • 48
1

To clarify Rohith Rajesh's answer, the jniortools library needed can be found inside the "ortools-(platform)-(version).jar" file. For mac osx it is in form of a "libjniortools.dylib" file.

You only need to extract the jar and point to it via java arguments (see Rohith Rajesh's answer)