aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Asim Shankar <ashankar@google.com>2017-03-27 17:35:03 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2017-03-27 20:56:21 -0700
commitad93dc71f1154c0a4935b1bd86653f29acc8fdfa (patch)
treeb48f39f3bbdb95fe6330f16a21a71ffc4638dc6c
parent2e28243adea19433513c18e32d47f803a7a4cfc2 (diff)
Java: Update documentation with instructions for Maven and Windows.
Fixes #7877 Fixes #6926 Change: 151400818
-rw-r--r--tensorflow/docs_src/install/install_java.md111
-rw-r--r--tensorflow/java/README.md120
2 files changed, 196 insertions, 35 deletions
diff --git a/tensorflow/docs_src/install/install_java.md b/tensorflow/docs_src/install/install_java.md
index 57d456059f..35a2d237e6 100644
--- a/tensorflow/docs_src/install/install_java.md
+++ b/tensorflow/docs_src/install/install_java.md
@@ -25,8 +25,99 @@ After installation, please see this
[complete example](https://www.tensorflow.org/code/tensorflow/examples/android)
of TensorFlow on Android.
+## Using TensorFlow with a Maven project
-## Install on Linux or Mac OS
+If your project uses [Apache Maven](https://maven.apache.org), then add the
+following to the project's `pom.xml` to use the TensorFlow Java APIs:
+
+```xml
+<dependency>
+ <groupId>org.tensorflow</groupId>
+ <artifactId>tensorflow</artifactId>
+ <version>1.1.0</version>
+</dependency>
+```
+
+That's all.
+
+### Example
+
+As an example, these steps will create a Maven project that uses TensorFlow:
+
+1. Create the project's `pom.xml`:
+
+ ```xml
+ <project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.myorg</groupId>
+ <artifactId>label-image</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <properties>
+ <exec.mainClass>HelloTF</exec.mainClass>
+ <!-- The sample code requires at least JDK 1.7. -->
+ <!-- The maven compiler plugin defaults to a lower version -->
+ <maven.compiler.source>1.7</maven.compiler.source>
+ <maven.compiler.target>1.7</maven.compiler.target>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>org.tensorflow</groupId>
+ <artifactId>tensorflow</artifactId>
+ <version>1.1.0</version>
+ </dependency>
+ </dependencies>
+ </project>
+ ```
+
+2. Create the source file (`src/main/java/HelloTF.java`):
+
+ ```java
+ import org.tensorflow.Graph;
+ import org.tensorflow.Session;
+ import org.tensorflow.Tensor;
+ import org.tensorflow.TensorFlow;
+
+ public class HelloTF {
+ public static void main(String[] args) throws Exception {
+ try (Graph g = new Graph()) {
+ final String value = "Hello from " + TensorFlow.version();
+
+ // Construct the computation graph with a single operation, a constant
+ // named "MyConst" with a value "value".
+ try (Tensor t = Tensor.create(value.getBytes("UTF-8"))) {
+ // The Java API doesn't yet include convenience functions for adding operations.
+ g.opBuilder("Const", "MyConst").setAttr("dtype", t.dataType()).setAttr("value", t).build();
+ }
+
+ // Execute the "MyConst" operation in a Session.
+ try (Session s = new Session(g);
+ Tensor output = s.runner().fetch("MyConst").run().get(0)) {
+ System.out.println(new String(output.bytesValue(), "UTF-8"));
+ }
+ }
+ }
+ }
+```
+
+3. Compile and execute:
+
+ ```bsh
+ # Use -q to hide logging from the mvn tool
+ mvn -q compile exec:java
+ ```
+
+The preceeding command should output `Hello from *version*`. If it does, you've
+succesfully set up TensorFlow for Java and are ready to use it in Maven
+projects. If not, check [Stack Overflow](http://stackoverflow.com/questions/tagged/tensorflow)
+for possible solutions. You can skip reading the rest of this document.
+
+## Using TensorFlow with JDK
+
+This section describes how to use TensorFlow using the `java` and `javac`
+commands from a JDK installation. If your project uses Apache Maven, then
+refer to the simpler instructions above instead.
+
+### Install on Linux or Mac OS
Take the following steps to install TensorFlow for Java on Linux or Mac OS:
@@ -45,7 +136,7 @@ Take the following steps to install TensorFlow for Java on Linux or Mac OS:
file for your operating system and processor support by running the
following shell commands:
- ```sh
+ ```bsh
TF_TYPE="cpu" # Default processor is CPU. If you want GPU, set to "gpu"
OS=$(uname -s | tr '[:upper:]' '[:lower:]')
mkdir -p ./jni
@@ -55,7 +146,7 @@ Take the following steps to install TensorFlow for Java on Linux or Mac OS:
```
-## Install on Windows
+### Install on Windows
Take the following steps to install TensorFlow for Java on Windows:
@@ -68,7 +159,7 @@ Take the following steps to install TensorFlow for Java on Windows:
-## Validate the installation
+### Validate the installation
After installing TensorFlow for Java, validate your installation by entering
the following code into a file named `HelloTF.java`:
@@ -101,30 +192,32 @@ public class HelloTF {
}
```
+And use the instructions below to compile and run `HelloTF.java`.
+
### Compiling
-When compiling a TensorFlow program written in Java, the downloaded `.jar`
+When compiling a Java program that uses TensorFlow, the downloaded `.jar`
must be part of your `classpath`. For example, you can include the
downloaded `.jar` in your `classpath` by using the `-cp` compilation flag
as follows:
-```sh
+```bsh
javac -cp libtensorflow-1.1.0.jar HelloTF.java
```
### Running
-To execute a TensorFlow program written in Java, ensure that the following
-two files are both in your `classpath`:
+To execute a Java program that depends on TensorFlow, ensure that the following
+two files are available to the JVM:
* the downloaded `.jar` file
* the extracted JNI library
For example, the following command line executes the `HelloTF` program:
-```sh
+```bsh
java -cp libtensorflow-1.1.0.jar:. -Djava.library.path=./jni HelloTF
```
diff --git a/tensorflow/java/README.md b/tensorflow/java/README.md
index 20eb6a8265..80c8f588ee 100644
--- a/tensorflow/java/README.md
+++ b/tensorflow/java/README.md
@@ -11,32 +11,95 @@ Java bindings for TensorFlow. ([Javadoc](https://www.tensorflow.org/api_docs/jav
> and/or the [Android
> demo](https://www.tensorflow.org/code/tensorflow/examples/android).
-## Quickstart
+## Quickstart: Using [Apache Maven](https://maven.apache.org)
+
+TensorFlow for Java releases are included in
+[Maven Central](https://search.maven.org/#search%7Cga%7C1%7Cg%3A%22org.tensorflow%22%20AND%20a%3A%22tensorflow%22)
+and support Linux, OS X and Windows. To use it, add the following dependency to
+your project's `pom.xml`:
+
+```xml
+<dependency>
+ <groupId>org.tensorflow</groupId>
+ <artifactId>tensorflow</artifactId>
+ <version>1.1.0-rc0-windows-fix</version>
+</dependency>
+```
+
+That's all. As an example, to create a Maven project for the
+[label image example](https://www.tensorflow.org/code/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java):
+
+1. Create a `pom.xml`:
+
+ ```xml
+<project>
+ <modelVersion>4.0.0</modelVersion>
+ <groupId>org.myorg</groupId>
+ <artifactId>label-image</artifactId>
+ <version>1.0-SNAPSHOT</version>
+ <properties>
+ <exec.mainClass>org.tensorflow.examples.LabelImage</exec.mainClass>
+ <!-- The LabelImage example code requires at least JDK 1.7. -->
+ <!-- The maven compiler plugin defaults to a lower version -->
+ <maven.compiler.source>1.7</maven.compiler.source>
+ <maven.compiler.target>1.7</maven.compiler.target>
+ </properties>
+ <dependencies>
+ <dependency>
+ <groupId>org.tensorflow</groupId>
+ <artifactId>tensorflow</artifactId>
+ <version>1.1.0-rc0-windows-fix</version>
+ </dependency>
+ </dependencies>
+</project>
+ ```
+
+2. Download the [example source](https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java)
+ into `src/main/java/org/tensorflow/examples`. On Linux and OS X, the following script should work:
+
+ ```sh
+ mkdir -p src/main/java/org/tensorflow/examples
+ curl -L "https://raw.githubusercontent.com/tensorflow/tensorflow/master/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java" -o src/main/java/org/tensorflow/examples/LabelImage.java
+ ```
+
+3. Compile and execute:
+
+ ```sh
+ mvn compile exec:java
+ ```
+
+## Quickstart: Using `java` and `javac`
+
+This section describes how to use TensorFlow armed with just a JDK installation.
1. Download the Java archive (JAR):
- [libtensorflow.jar](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-1.0.0-PREVIEW1.jar)
+ [libtensorflow.jar](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-1.1.0-rc0.jar)
(optionally, the Java sources:
- [libtensorflow-src.jar](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-src-1.0.0-PREVIEW1.jar)).
+ [libtensorflow-src.jar](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-src-1.1.0-rc0.jar)).
2. 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](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-linux-x86_64-1.0.0-PREVIEW1.tar.gz),
- [GPU-enabled](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-gpu-linux-x86_64-1.0.0-PREVIEW1.tar.gz)
+ [CPU-only](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-linux-x86_64-1.1.0-rc0.tar.gz),
+ [GPU-enabled](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-gpu-linux-x86_64-1.1.0-rc0.tar.gz)
- OS X:
- [CPU-only](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-darwin-x86_64-1.0.0-PREVIEW1.tar.gz),
- [GPU-enabled](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-gpu-darwin-x86_64-1.0.0-PREVIEW1.tar.gz)
+ [CPU-only](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-darwin-x86_64-1.1.0-rc0.tar.gz),
+ [GPU-enabled](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-gpu-darwin-x86_64-1.1.0-rc0.tar.gz)
+ - Windows:
+ [CPU-only](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-windows-x86_64-1.1.0-rc0.zip)
+
- The following shell snippet downloads and extracts the native library:
+ The following shell snippet downloads and extracts the native library on
+ Linux and OS X. For Windows, download and extract manually.
```sh
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" |
+ "https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-${TF_TYPE}-${OS}-x86_64-1.1.0-rc0.tar.gz" |
tar -xz -C ./jni
```
@@ -56,7 +119,7 @@ Java bindings for TensorFlow. ([Javadoc](https://www.tensorflow.org/api_docs/jav
then it should be compiled with:
```sh
- javac -cp libtensorflow-1.0.0-PREVIEW1.jar MyClass.java
+ javac -cp libtensorflow-1.1.0-rc0.jar MyClass.java
```
For a more sophisticated example, see
@@ -65,7 +128,7 @@ Java bindings for TensorFlow. ([Javadoc](https://www.tensorflow.org/api_docs/jav
```sh
javac \
- -cp libtensorflow-1.0.0-PREVIEW1.jar \
+ -cp libtensorflow-1.1.0-rc0.jar \
./src/main/java/org/tensorflow/examples/LabelImage.java
```
@@ -73,7 +136,7 @@ Java bindings for TensorFlow. ([Javadoc](https://www.tensorflow.org/api_docs/jav
library path during execution. For example:
```sh
- java -cp libtensorflow-1.0.0-PREVIEW1.jar:. -Djava.library.path=./jni MyClass
+ java -cp libtensorflow-1.1.0-rc0.jar:. -Djava.library.path=./jni MyClass
```
or for the `LabelImage` example:
@@ -81,17 +144,14 @@ Java bindings for TensorFlow. ([Javadoc](https://www.tensorflow.org/api_docs/jav
```sh
java \
-Djava.library.path=./jni \
- -cp libtensorflow-1.0.0-PREVIEW1.jar:./src/main/java \
+ -cp libtensorflow-1.1.0-rc0.jar:./src/main/java \
org.tensorflow.examples.LabelImage
```
-That's all. These artifacts are not yet available on Maven central, see
-[#6926](https://github.com/tensorflow/tensorflow/issues/6926).
-
## Building from source
-If the quickstart instructions above do not work out, the TensorFlow native
-libraries will need to be built 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](https://www.bazel.build/versions/master/docs/install.html)
@@ -110,6 +170,7 @@ libraries will need to be built from source.
brew install swig
```
+
3. [Configure](https://www.tensorflow.org/install/install_sources#configure_the_installation)
(e.g., enable GPU support) and build:
@@ -120,14 +181,22 @@ libraries will need to be built from source.
//tensorflow/java:libtensorflow_jni
```
-The JAR (`libtensorflow.jar`) and native library (`libtensorflow_jni.so` on Linux or `libtensorflow_jni.dylib` on OS X) will
-be in `bazel-bin/tensorflow/java`. Using these artifacts follow both steps 3 and 4 in the [quickstart](#quickstart) section in order to get your application up and running.
+The JAR (`libtensorflow.jar`) and native library (`libtensorflow_jni.so` on
+Linux, `libtensorflow_jni.dylib` on OS X, `tensorflow_jni.dll` on Windows) will
+be in `bazel-bin/tensorflow/java`. Using these artifacts follow both steps 3
+and 4 in the previous section in order to get your application
+up and running.
+
+Installation on Windows requires the more experimental [bazel on Windows](https://bazel.build/versions/master/docs/windows.html).
+Details are elided here, but find inspiration in the script used for
+building the release archive:
+[`tensorflow/tools/ci_build/windows/libtensorflow_cpu.sh`](https://www.tensorflow.org/code/tensorflow/tools/ci_build/windows/libtensorflow_cpu.sh).
### Maven
-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):
+Details of the release process for Maven Central are in [`maven/README.md`](https://www.tensorflow.org/code/tensorflow/java/maven/README.md).
+However, for development, you can push the library built from source to a local
+Maven repository with:
```sh
bazel build -c opt //tensorflow/java:pom
@@ -136,9 +205,8 @@ mvn install:install-file \
-DpomFile=../../bazel-bin/tensorflow/java/pom.xml
```
-Refer 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).
+And then rever to this library in a project's `pom.xml` with:
+(replacing 1.0.head with the appropriate version):
```xml
<dependency>