Java bindings for TensorFlow. (Javadoc)
WARNING: The TensorFlow Java API is not currently covered by the TensorFlow API stability guarantees.
For using TensorFlow on Android refer to contrib/android, makefile and/or the Android demo.
-
Download the Java archive (JAR): libtensorflow.jar (optionally, the Java sources: libtensorflow-src.jar).
-
Download the native library. GPU-enabled versions required CUDA 8 and cuDNN 5.1. For other versions, the native library will need to be built from source (see below).
- Linux: CPU-only, GPU-enabled
- OS X: CPU-only, GPU-enabled
The following shell snippet downloads and extracts the native library:
TF_TYPE="cpu" # Set to "gpu" to enable GPU support OS=$(uname -s | tr '[:upper:]' '[:lower:]') mkdir -p ./jni curl -L \ "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-${TF_TYPE}-${OS}-x86_64-1.0.0-PREVIEW1.tar.gz" | tar -xz -C ./jni
-
Include the downloaded
.jarin the classpath during compilation. For example, if your program looks like the following:import org.tensorflow.TensorFlow; public class MyClass { public static void main(String[] args) { System.out.println("I'm using TensorFlow version: " + TensorFlow.version()); } }
then it should be compiled with:
javac -cp libtensorflow-1.0.0-PREVIEW1.jar MyClass.java
For a more sophisticated example, see LabelImage.java, which can be compiled with:
javac \ -cp libtensorflow-1.0.0-PREVIEW1.jar \ ./src/main/java/org/tensorflow/examples/LabelImage.java
-
Include the downloaded
.jarin the classpath and the native library in the library path during execution. For example:java -cp libtensorflow-1.0.0-PREVIEW1.jar:. -Djava.library.path=./jni MyClass
or for the
LabelImageexample:java \ -Djava.library.path=./jni \ -cp libtensorflow-1.0.0-PREVIEW1.jar:./src/main/java \ org.tensorflow.examples.LabelImage
That's all. These artifacts are not yet available on Maven central, see #6926.
If the quickstart instructions above do not work out, the TensorFlow native libraries will need to be built from source.
-
Install bazel
-
Setup the environment to buile TensorFlow from source code (Linux or Mac OS X). If you'd like to skip reading those details and do not care about GPU support, try the following:
# On Linux sudo apt-get install python swig python-numpy # On Mac OS X with homebrew brew install swig
-
Configure (e.g., enable GPU support) and build:
./configure bazel build --config opt \ //tensorflow/java:tensorflow \ //tensorflow/java:libtensorflow_jni
The JAR (libtensorflow.jar) and native library (libtensorflow_jni.so) will
be in bazel-genfiles/tensorflow/tensorflow/java.
To use the library in an external Java project, publish the library to a Maven
repository. For example, publish the library to the local Maven repository using
the mvn tool (installed separately):
bazel build -c opt //tensorflow/java:pom
mvn install:install-file \
-Dfile=../../bazel-bin/tensorflow/java/libtensorflow.jar \
-DpomFile=../../bazel-bin/tensorflow/java/pom.xmlRefer to the library using Maven coordinates. For example, if you're using Maven
then place this dependency into your pom.xml file (replacing 1.0.head with
the version of the TensorFlow runtime you wish to use).
<dependency>
<groupId>org.tensorflow</groupId>
<artifactId>libtensorflow</artifactId>
<version>1.0.head</version>
</dependency>If your project uses bazel for builds, add a dependency on
//tensorflow/java:tensorflow to the java_binary or java_library rule. For
example:
bazel run -c opt //tensorflow/java/src/main/java/org/tensorflow/examples:label_image