Adding Tensorflow to Java Project Netbeans

2019-04-02 16:43发布

I am trying to install Tensorflow for java based on the recommendations at this link...

https://www.tensorflow.org/install/install_java#install_on_windows

The instructions state to download the .jar file, and a separate file for a .dll. I have included the jar file into the netbeans project, and setup the code listed on the above webpage.

package tensorflowtest;
import org.tensorflow.Graph;
import org.tensorflow.Session;
import org.tensorflow.Tensor;
import org.tensorflow.TensorFlow;


public class TensorFlowTest {

    public static void main(String[] args) throws Exception {
    try (Graph g = new Graph()) {
      final String value = "Hello from " + TensorFlow.version();

      // Construct the computation graph with a single operation, a constant
      // named "MyConst" with a value "value".
      try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
        // The Java API doesn't yet include convenience functions for adding operations.
        g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
      }

      // Execute the "MyConst" operation in a Session.
      try (Session s = new Session(g);
           Tensor output = s.runner().fetch("MyConst").run().get(0)) {
        System.out.println(new String(output.bytesValue(), "UTF-8"));
      }
    }
  }

} 

When Running this, I get the error

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: windows, architecture: x86. See https://github.com/tensorflow/tensorflow/tree/master/java/README.md for possible solutions (such as building the library from source).
    at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
    at org.tensorflow.TensorFlow.init(TensorFlow.java:27)
    at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:31)
    at org.tensorflow.Graph.<clinit>(Graph.java:194)
    at tensorflowtest.TensorFlowTest.main(TensorFlowTest.java:11)

I know this is because the .dll file is not being found, but I have tried placing the .dll in all the root files, I have tried adding the dll to my sources and/or libraries, and I have tried adding the command -Djava.library.path=. to my VM options in Netbeans and tried adding System.setProperty("java.library.path", "."); to the beginning of my main function, all without success. I have also tried giving direct paths to the dll in the form of "C:\Path\To\File"

Any suggestions on how to fix this this would be appreciated.

2条回答
Summer. ? 凉城
2楼-- · 2019-04-02 16:49

If the file were located in the directory where it was run it should have worked in the first place (Am I missing something?) Setting the correct absolute path under VM Options worked for me:

-Djava.library.path="C:\Program Files\Java\tensorflow"

This way you dont have to copy the dlls back and forth between your projects.

See also: giving 'java.library.path' in netbeans for .dll/.so files

查看更多
【Aperson】
3楼-- · 2019-04-02 17:05

Looks like it works to put the tensorflow_jni.dll file in the root directory of your project folder as mentioned here:

http://wiki.netbeans.org/SlickSet

查看更多
登录 后发表回答