README.md
1 # TensorFlow for Java
2
3 > *WARNING*: The TensorFlow Java API is not currently covered by the TensorFlow
4 > [API stability guarantees](https://www.tensorflow.org/guide/version_compat).
5 >
6 > For using TensorFlow on Android refer instead to
7 > [contrib/android](https://www.tensorflow.org/code/tensorflow/contrib/android),
8 > [makefile](https://www.tensorflow.org/code/tensorflow/contrib/makefile#android)
9 > and/or the
10 > [Android demo](https://www.tensorflow.org/code/tensorflow/examples/android).
11
12 ## Quickstart
13
14 - Refer to [Installing TensorFlow for Java](https://www.tensorflow.org/install/lang_java)
15 - [Javadoc](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/package-summary)
16 - [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.tensorflow/tensorflow/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.tensorflow/tensorflow)
17
18 ## Nightly builds
19
20 Releases built from release branches are available on Maven Central.
21 Additionally, every day binaries are built from the `master` branch on GitHub:
22
23 - [JAR](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow.jar)
24 - [Source JAR](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow-src.jar)
25 - JNI:
26 - [Linux CPU-only](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow_jni-cpu-linux-x86_64.tar.gz)
27 - [Linux GPU](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow_jni-gpu-linux-x86_64.tar.gz)
28 - [MacOS](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow_jni-cpu-darwin-x86_64.tar.gz)
29 - Windows: (No nightly builds available yet)
30
31 ## Building from source
32
33 If the quickstart instructions above do not work out, the TensorFlow Java and
34 native libraries will need to be built from source.
35
36 1. Install [bazel](https://www.bazel.build/versions/master/docs/install.html)
37
38 2. Setup the environment to build TensorFlow from source code
39 ([Linux or macOS](https://www.tensorflow.org/install/source)).
40 If you'd like to skip reading those details and do not care about GPU
41 support, try the following:
42
43 ```sh
44 # On Linux
45 sudo apt-get install python swig python-numpy
46
47 # On Mac OS X with homebrew
48 brew install swig
49 ```
50
51 3. [Configure](https://www.tensorflow.org/install/source)
52 (e.g., enable GPU support) and build:
53
54 ```sh
55 ./configure
56 bazel build --config opt \
57 //tensorflow/java:tensorflow \
58 //tensorflow/java:libtensorflow_jni
59 ```
60
61 The command above will produce two files in the `bazel-bin/tensorflow/java`
62 directory:
63
64 * An archive of Java classes: `libtensorflow.jar`
65 * A native library: `libtensorflow_jni.so` on Linux, `libtensorflow_jni.dylib`
66 on OS X, or `tensorflow_jni.dll` on Windows.
67
68 To compile Java code that uses the TensorFlow Java API, include
69 `libtensorflow.jar` in the classpath. For example:
70
71 ```sh
72 javac -cp bazel-bin/tensorflow/java/libtensorflow.jar ...
73 ```
74
75 To execute the compiled program, include `libtensorflow.jar` in the classpath
76 and the native library in the library path. For example:
77
78 ```sh
79 java -cp bazel-bin/tensorflow/java/libtensorflow.jar \
80 -Djava.library.path=bazel-bin/tensorflow/java \
81 ...
82 ```
83
84 Installation on Windows requires the more experimental [bazel on
85 Windows](https://bazel.build/versions/master/docs/windows.html). Details are
86 omitted here, but find inspiration in the script used for building the release
87 archive:
88 [`tensorflow/tools/ci_build/windows/libtensorflow_cpu.sh`](https://www.tensorflow.org/code/tensorflow/tools/ci_build/windows/libtensorflow_cpu.sh).
89
90 ### Maven
91
92 Details of the release process for Maven Central are in
93 [`maven/README.md`](https://www.tensorflow.org/code/tensorflow/java/maven/README.md).
94 However, for development, you can push the library built from source to a local
95 Maven repository with:
96
97 ```sh
98 bazel build -c opt //tensorflow/java:pom
99 mvn install:install-file \
100 -Dfile=../../bazel-bin/tensorflow/java/libtensorflow.jar \
101 -DpomFile=../../bazel-bin/tensorflow/java/pom.xml
102 ```
103
104 And then refer to this library in a project's `pom.xml` with: (replacing
105 VERSION with the appropriate version of TensorFlow):
106
107 ```xml
108 <dependency>
109 <groupId>org.tensorflow</groupId>
110 <artifactId>libtensorflow</artifactId>
111 <version>VERSION</version>
112 </dependency>
113 ```
114
115 ### Bazel
116
117 If your project uses bazel for builds, add a dependency on
118 `//tensorflow/java:tensorflow` to the `java_binary` or `java_library` rule. For
119 example:
120
121 ```sh
122 bazel run -c opt //tensorflow/java/src/main/java/org/tensorflow/examples:label_image
123 ```
124