• Home
Name Date Size #Lines LOC

..--

config/03-May-2024-1613

src/03-May-2024-18,38911,218

BUILDD03-May-202411.2 KiB470429

README.mdD03-May-20243.7 KiB9570

build_defs.bzlD03-May-20246.3 KiB158150

generate_pom.ccD03-May-20242 KiB6138

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 [TensorFlow Lite](https://www.tensorflow.org/code/tensorflow/lite/).
7
8## Quickstart
9
10-   Refer to [Installing TensorFlow for Java](https://www.tensorflow.org/install/lang_java)
11-   [Javadoc](https://www.tensorflow.org/api_docs/java/reference/org/tensorflow/package-summary)
12-   [![Maven Central](https://maven-badges.herokuapp.com/maven-central/org.tensorflow/tensorflow/badge.svg)](https://maven-badges.herokuapp.com/maven-central/org.tensorflow/tensorflow)
13
14## Nightly builds
15
16Releases built from release branches are available on Maven Central.
17Additionally, every day binaries are built from the `master` branch on GitHub:
18
19-   [JAR](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow.jar)
20-   [Source JAR](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow-src.jar)
21-   JNI:
22    -   [Linux CPU-only](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow_jni-cpu-linux-x86_64.tar.gz)
23    -   [Linux GPU](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow_jni-gpu-linux-x86_64.tar.gz)
24    -   [MacOS](https://storage.googleapis.com/tensorflow-nightly/github/tensorflow/lib_package/libtensorflow_jni-cpu-darwin-x86_64.tar.gz)
25    -   Windows: (No nightly builds available yet)
26
27## Building from source
28
29If the quickstart instructions above do not work out, the TensorFlow Java and
30native libraries will need to be built from source.
31
321.  Install [bazel](https://www.bazel.build/versions/master/docs/install.html)
33
342.  Setup the environment to build TensorFlow from source code
35    ([Linux or macOS](https://www.tensorflow.org/install/source)).
36    If you'd like to skip reading those details and do not care about GPU
37    support, try the following:
38
39    ```sh
40    # On Linux
41    sudo apt-get install python swig python-numpy
42
43    # On Mac OS X with homebrew
44    brew install swig
45    ```
46
473.  [Configure](https://www.tensorflow.org/install/source)
48    (e.g., enable GPU support) and build:
49
50    ```sh
51    ./configure
52    bazel build --config opt \
53      //tensorflow/java:tensorflow \
54      //tensorflow/java:libtensorflow_jni
55    ```
56
57The command above will produce two files in the `bazel-bin/tensorflow/java`
58directory:
59
60*   An archive of Java classes: `libtensorflow.jar`
61*   A native library: `libtensorflow_jni.so` on Linux, `libtensorflow_jni.dylib`
62    on OS X, or `tensorflow_jni.dll` on Windows.
63
64To compile Java code that uses the TensorFlow Java API, include
65`libtensorflow.jar` in the classpath. For example:
66
67```sh
68javac -cp bazel-bin/tensorflow/java/libtensorflow.jar ...
69```
70
71To execute the compiled program, include `libtensorflow.jar` in the classpath
72and the native library in the library path. For example:
73
74```sh
75java -cp bazel-bin/tensorflow/java/libtensorflow.jar \
76  -Djava.library.path=bazel-bin/tensorflow/java \
77  ...
78```
79
80Installation on Windows requires the more experimental [bazel on
81Windows](https://bazel.build/versions/master/docs/windows.html). Details are
82omitted here, but find inspiration in the script used for building the release
83archive:
84[`tensorflow/tools/ci_build/windows/libtensorflow_cpu.sh`](https://www.tensorflow.org/code/tensorflow/tools/ci_build/windows/libtensorflow_cpu.sh).
85
86### Bazel
87
88If your project uses bazel for builds, add a dependency on
89`//tensorflow/java:tensorflow` to the `java_binary` or `java_library` rule. For
90example:
91
92```sh
93bazel run -c opt //tensorflow/java/src/main/java/org/tensorflow/examples:label_image
94```
95