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