tensorflow / java

Java bindings for TensorFlow
Apache License 2.0
831 stars 202 forks source link

Simple Project Not Working #374

Open tomaspiaggio opened 3 years ago

tomaspiaggio commented 3 years ago

Host description

Simple project to replicate the problem

Problem description

The main method is straight out of the docs here. I can't seem to make it work. My main language of choice is Scala. I was trying to make it work in sbt and was getting a similar exception. From what I searched on the internet, it seems to be a JavaCPP error (missing *-platform). I don't think I'm doing anything wrong as I copied the example project. Please correct my if I'm wrong.

Warning: Could not load Loader: java.lang.UnsatisfiedLinkError: no jnijavacpp in java.library.path
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jnitensorflow 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 org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1683)
    at org.bytedeco.javacpp.Loader.load(Loader.java:1300)
    at org.bytedeco.javacpp.Loader.load(Loader.java:1123)
    at org.tensorflow.internal.c_api.global.tensorflow.<clinit>(tensorflow.java:12)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)
    at org.bytedeco.javacpp.Loader.load(Loader.java:1190)
    at org.bytedeco.javacpp.Loader.load(Loader.java:1139)
    at org.tensorflow.NativeLibrary.load(NativeLibrary.java:48)
    at org.tensorflow.TensorFlow.init(TensorFlow.java:111)
    at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:126)
    at my.project.App.main(App.java:19)
Caused by: java.lang.UnsatisfiedLinkError: no tensorflow_cc 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 org.bytedeco.javacpp.Loader.loadLibrary(Loader.java:1683)
    at org.bytedeco.javacpp.Loader.load(Loader.java:1227)
    ... 10 more
tomaspiaggio commented 3 years ago

I just tested the same code on my mac and it worked perfectly. Could there be an issue with Windows specifically?

Craigacp commented 3 years ago

Did Maven download tensorflow-core-api-0.3.1-windows-x86_64.jar to your local Maven cache? That jar should contain jnitensorflow.dll and tensorflow_cc.dll. It's available here - https://repo1.maven.org/maven2/org/tensorflow/tensorflow-core-api/0.3.1/.

tomaspiaggio commented 3 years ago

I just checked and they are being correctly downloaded:

Screenshot 2021-08-25 151559

The files were contained in this folder structure:

Screenshot 2021-08-25 151628

Craigacp commented 3 years ago

Does it end up in the classpath that Maven/your IDE generates when you run the program?

tomaspiaggio commented 3 years ago

Running these lines

Arrays.stream(((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs())
    .map(e -> e.getFile())
    .forEach(System.out::println);

yields the following output

/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/charsets.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/deploy.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/access-bridge-32.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/cldrdata.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/dnsns.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/jaccess.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/jfxrt.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/localedata.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/nashorn.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/sunec.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/sunjce_provider.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/sunmscapi.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/sunpkcs11.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/ext/zipfs.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/javaws.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/jce.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/jfr.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/jfxswt.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/jsse.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/management-agent.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/plugin.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/resources.jar
/C:/Program%20Files/Java/jdk1.8.0_261/jre/lib/rt.jar
/C:/Users/[...]/target/classes/
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-platform/0.3.1/tensorflow-core-platform-0.3.1.jar
/C:/Users/Tomas/.m2/repository/org/bytedeco/javacpp/1.5.4/javacpp-1.5.4.jar
/C:/Users/Tomas/.m2/repository/org/bytedeco/javacpp/1.5.4/javacpp-1.5.4-linux-x86_64.jar
/C:/Users/Tomas/.m2/repository/org/bytedeco/javacpp/1.5.4/javacpp-1.5.4-macosx-x86_64.jar
/C:/Users/Tomas/.m2/repository/org/bytedeco/javacpp/1.5.4/javacpp-1.5.4-windows-x86_64.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-api/0.3.1/tensorflow-core-api-0.3.1.jar
/C:/Users/Tomas/.m2/repository/com/google/protobuf/protobuf-java/3.8.0/protobuf-java-3.8.0.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/ndarray/0.3.1/ndarray-0.3.1.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-api/0.3.1/tensorflow-core-api-0.3.1-linux-x86_64.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-api/0.3.1/tensorflow-core-api-0.3.1-macosx-x86_64.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-api/0.3.1/tensorflow-core-api-0.3.1-windows-x86_64.jar
/C:/Program%20Files/JetBrains/IntelliJ%20IDEA%202020.2/lib/idea_rt.jar

Filtering with "tensor" keyword like this

Arrays.stream(((URLClassLoader) ClassLoader.getSystemClassLoader()).getURLs())
    .map(e -> e.getFile())
    .filter(e -> e.indexOf("tensor") > 0)
    .forEach(System.out::println);

results in the following

/C:/Users/[...]/target/classes/
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-platform/0.3.1/tensorflow-core-platform-0.3.1.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-api/0.3.1/tensorflow-core-api-0.3.1.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/ndarray/0.3.1/ndarray-0.3.1.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-api/0.3.1/tensorflow-core-api-0.3.1-linux-x86_64.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-api/0.3.1/tensorflow-core-api-0.3.1-macosx-x86_64.jar
/C:/Users/Tomas/.m2/repository/org/tensorflow/tensorflow-core-api/0.3.1/tensorflow-core-api-0.3.1-windows-x86_64.jar
Craigacp commented 3 years ago

Ok. Comparing the published artifacts for tensorflow-core-api it looks like the Windows one is missing tensorflow_framework, but I'm not sure if that's necessary on Windows as it might be compiled into the other binaries. @saudet do you know why the set of Windows dlls that are packaged is different?

saudet commented 3 years ago

@tomaspiaggio There's probably some missing dependency somewhere. I've bundled a couple more DLLs in JavaCPP 1.5.6, so please try to upgrade javacpp-platform and see what that gives. Other than that, we can usually figure out what it's looking for with tools like Dependencies: https://github.com/bytedeco/javacpp-presets/wiki/Debugging-UnsatisfiedLinkError-on-Windows

@Craigacp It's not possible to export all symbols on Windows, so the C++ API is limited there, that's normal.

rnett commented 3 years ago

Your project works fine for me, and I've been able to use tensorflow java in other projects, so this is likely something on your end. Try removing the ~/.m2/org/tensorflow directory maybe? And make sure you're building and running through maven, not IntelliJ (see this).