将Tensorflow添加到Java Project Netbeans [英] Adding Tensorflow to Java Project Netbeans
问题描述
我正在尝试根据此链接中的建议安装Tensorflow for java ...
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
说明state下载.jar文件,以及.dll的单独文件。我已将jar文件包含到netbeans项目中,并设置上面网页上列出的代码。
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"));
}
}
}
}
运行此时,我收到错误
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的直接路径。
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.
推荐答案
如果文件位于运行它的目录中,它应该首先工作(我错过了什么?)
在VM选项下设置正确的绝对路径对我有用:
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.
参见:在netbeans中为.dll / .so文件提供'java.library.path'
See also: giving 'java.library.path' in netbeans for .dll/.so files
这篇关于将Tensorflow添加到Java Project Netbeans的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!