我正在尝试根据此链接中的建议安装Tensorflow for java ...
https://www.tensorflow.org/install/install_java#install_on_windows
说明指出要下载.jar文件,以及.dll的单独文件。我已将jar文件包含到netbeans项目中,并设置上面网页上列出的代码。
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"));
}
}
}
}
运行此时,我收到错误
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)
我知道这是因为找不到.dll文件,但我尝试将.dll放在所有根文件中,我尝试将dll添加到我的源和/或库中,我尝试添加命令-Djava.library.path =。到我在Netbeans中的VM选项,并尝试添加System.setProperty(“java.library.path”,“。”);到我的主要功能的开头,都没有成功。我也尝试以“C:\ Path \ To \ File”
的形式提供到dll的直接路径如何解决这个问题的任何建议将不胜感激。
答案 0 :(得分:2)
如果该文件位于运行目录中,则它应该首先就可以正常工作(我遗漏了什么吗?) 在“虚拟机选项”下设置正确的绝对路径对我有用:
-Djava.library.path="C:\Program Files\Java\tensorflow"
通过这种方式,您不必在项目之间来回复制dll。
另请参阅:giving 'java.library.path' in netbeans for .dll/.so files
答案 1 :(得分:1)
看起来像将tensorflow_jni.dll文件放在项目文件夹的根目录中一样,如下所述: