STT-tensorflow/tensorflow/java
Liam Miller-Cushon 2e02ff669a Explicitly disable javac -parameters metadata for Java 7 compilations
PiperOrigin-RevId: 314185291
Change-Id: I8688c8dfd65680bfeffd5832d3826f013af96d9d
2020-06-01 12:56:57 -07:00
..
config
maven Merge pull request #39384 from yongtang:39381-protobuf-java 2020-05-14 18:54:55 -07:00
src Resolve trivial aliases for portable TensorFlow targets. 2020-05-14 09:39:36 -07:00
BUILD
README.md
build_defs.bzl Explicitly disable javac -parameters metadata for Java 7 compilations 2020-06-01 12:56:57 -07:00
generate_pom.cc

README.md

TensorFlow for Java

WARNING: The TensorFlow Java API is not currently covered by the TensorFlow API stability guarantees.

For using TensorFlow on Android refer instead to TensorFlow Lite.

Quickstart

Nightly builds

Releases built from release branches are available on Maven Central. Additionally, every day binaries are built from the master branch on GitHub:

Building from source

If the quickstart instructions above do not work out, the TensorFlow Java and native libraries will need to be built from source.

  1. Install bazel

  2. Setup the environment to build TensorFlow from source code (Linux or macOS). 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
    
  3. Configure (e.g., enable GPU support) and build:

    ./configure
    bazel build --config opt \
      //tensorflow/java:tensorflow \
      //tensorflow/java:libtensorflow_jni
    

The command above will produce two files in the bazel-bin/tensorflow/java directory:

  • An archive of Java classes: libtensorflow.jar
  • A native library: libtensorflow_jni.so on Linux, libtensorflow_jni.dylib on OS X, or tensorflow_jni.dll on Windows.

To compile Java code that uses the TensorFlow Java API, include libtensorflow.jar in the classpath. For example:

javac -cp bazel-bin/tensorflow/java/libtensorflow.jar ...

To execute the compiled program, include libtensorflow.jar in the classpath and the native library in the library path. For example:

java -cp bazel-bin/tensorflow/java/libtensorflow.jar \
  -Djava.library.path=bazel-bin/tensorflow/java \
  ...

Installation on Windows requires the more experimental bazel on Windows. Details are omitted here, but find inspiration in the script used for building the release archive: tensorflow/tools/ci_build/windows/libtensorflow_cpu.sh.

Maven

Details of the release process for Maven Central are in maven/README.md. However, for development, you can push the library built from source to a local Maven repository with:

bazel build -c opt //tensorflow/java:pom
mvn install:install-file \
  -Dfile=../../bazel-bin/tensorflow/java/libtensorflow.jar \
  -DpomFile=../../bazel-bin/tensorflow/java/pom.xml

And then refer to this library in a project's pom.xml with: (replacing VERSION with the appropriate version of TensorFlow):

<dependency>
  <groupId>org.tensorflow</groupId>
  <artifactId>libtensorflow</artifactId>
  <version>VERSION</version>
</dependency>

Bazel

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