java-demo icon indicating copy to clipboard operation
java-demo copied to clipboard

java.lang.UnsatisfiedLinkError: no pytorch_jni in java.library.path

Open dzlab opened this issue 3 years ago • 1 comments

I am trying to build this demo project on mac, I installed libtorch with brew

$ brew install libtorch

This installed libtorch 1.10.0 in /usr/local/Cellar/libtorch/1.10.0 there was no build-version file so I created one with the version like this

cat > /usr/local/Cellar/libtorch/1.10.0/build-version << EOF
1.10.0
EOF

Then had to modify build.gradle#L8 to match my installed version

- def LIBTORCH_VERSION = USE_NIGHTLY ? "1.10.0-SNAPSHOT" : "1.9.0";
+ def LIBTORCH_VERSION = USE_NIGHTLY ? "1.10.0-SNAPSHOT" : "1.10.0";

The I export the libtorch path

$ export LIBTORCH_HOME=/usr/local/Cellar/libtorch/1.10.0

Now when I try to build the demo, it fails the pytorch_jni file

$ ./gradlew run   

> Task :run FAILED
Exception in thread "main" java.lang.UnsatisfiedLinkError: no pytorch_jni in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
        at java.lang.Runtime.loadLibrary0(Runtime.java:871)
        at java.lang.System.loadLibrary(System.java:1122)
        at com.facebook.soloader.nativeloader.SystemDelegate.loadLibrary(SystemDelegate.java:22)
        at com.facebook.soloader.nativeloader.NativeLoader.loadLibrary(NativeLoader.java:43)
        at org.pytorch.NativePeer.<clinit>(NativePeer.java:10)
        at org.pytorch.Module.load(Module.java:28)
        at org.pytorch.Module.load(Module.java:38)
        at demo.App.main(App.java:11)

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_271.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

@dreiss any ideas??

dzlab avatar Dec 16 '21 02:12 dzlab

Maybe you can use java -XshowSettings to check the java.library.path

If you use windows as dev platform, you can add the libtorch/lib to you envrionment variable.

If you use other platform such as linux, you can use the command java -Djava.library.path=xxx -cp xx.jar MainClass to run code after build jars .

cgpeter96 avatar Jan 27 '22 07:01 cgpeter96