title: Access Tensorflow from Tomcat on CentOS Linux
body: <p>I have a <a href="https://www.tensorflow.org/install/install_java" rel="nofollow noreferrer">Java demo working</a> that uses Tensorflow for image classification. It runs okay on Windows, but now I want to run it as a web service from the Java Tomcat web server.</p>
  
  <p>I have added all the Tensorflow jars to Tomcat's lib, but Tensorflow has a jni dependency. I'm not sure how to install and link this so Tensorflow can run on the CentOS Linux server.</p>
  
  <p><a href="https://www.tensorflow.org/install/install_linux" rel="nofollow noreferrer">I have read this</a>, but I do not need to run python on the server, just access Tensorflow from Java.</p>
  
  <p>Update: **Okay, to get this to work on Tomcat on Windows I do the following,</p>
  
  <p>download libtensorflow.jar from,
  <a href="https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-1.6.0.jar" rel="nofollow noreferrer">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-1.6.0.jar</a></p>
  
  <p>and then the dll from,
  <a href="https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-windows-x86_64-1.6.0.zip" rel="nofollow noreferrer">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-windows-x86_64-1.6.0.zip</a>
  (extract zip to get dll)</p>
  
  <p>See, <a href="https://www.tensorflow.org/install/install_java" rel="nofollow noreferrer">https://www.tensorflow.org/install/install_java</a></p>
  
  <p>put the jar in my tomcat lib, and create a tomcat dll dir and put the dll in it</p>
  
  <p>edit my setenv.bat and add the line,</p>
  
  <pre><code>SET CATALINA_OPTS=-Xmx4g -XX:PermSize=128m -XX:MaxPermSize=512m -Djava.library.path=D:\Engineering\apache-tomcat-7.0.50\dll
  </code></pre>
  
  <p>This works on Windows.</p>
  
  <p>For Linux, CentOS 6, I do the same, but instead of the dll download the so files from,
  <a href="https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-linux-x86_64-1.6.0.tar.gz" rel="nofollow noreferrer">https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow_jni-cpu-linux-x86_64-1.6.0.tar.gz</a></p>
  
  <p>and edit my setenv.sh and add the lines,</p>
  
  <pre><code>export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/tomcat8/so"
  export JAVA_OPTS="-server -Xmx38g -Djava.library.path=/usr/local/tomcat8/so"
  export CATALINA_OPTS="-Djava.library.path=/usr/local/tomcat8/so"
  </code></pre>
  
  <p>But none of these seem to work, I always get the error,</p>
  
  <blockquote>
    <p>Cannot find TensorFlow native library for OS: linux, architecture:
    x86_64. See
    <a href="https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md" rel="nofollow noreferrer">https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md</a>
    for possible solutions (such as building the library from source).
    Additional information on attempts to find the native library can be
    obtained by adding org.tensorflow.NativeLibrary.DEBUG=1 to the system
    properties of the JVM.</p>
  </blockquote>
  
  <p>I found there is another deployment option by instead just adding the jar,</p>
  
  <p>to lib, and it will magically find the correct so files.</p>
  
  <p><a href="https://mvnrepository.com/artifact/org.tensorflow/libtensorflow_jni" rel="nofollow noreferrer">https://mvnrepository.com/artifact/org.tensorflow/libtensorflow_jni</a></p>
  
  <p>When I try this option it seems to find the so files, but I get this error,</p>
  
  <blockquote>
    <p>/usr/local/tomcat8/temp/tensorflow_native_libraries-1522357321965-0/libtensorflow_jni.so: /lib64/libc.so.6: version `GLIBC_2.16' not found (required by /usr/local/tomcat8/temp/tensorflow_native_libraries-1522357321965-0/libtensorflow_jni.so)</p>
  </blockquote>
  
  <p>Seems like Tensorflow only supports a very specific OS and version??</p>
  
  <p>I found this,
  <a href="https://stackoverflow.com/questions/33655731/error-while-importing-tensorflow-in-python2-7-in-ubuntu-12-04-glibc-2-17-not-f">Error while importing Tensorflow in python2.7 in Ubuntu 12.04. &#39;GLIBC_2.17 not found&#39;</a></p>
  
  <p>But have not tried any of the options yet. Does not look promising for a production system.</p>
  
  <p>Looking at what GLIBC is, it is for GPU, but I don't have or need to use a GPU, just want to use the CPU, why is this library required??</p>
  
  <p>** Update
  So... I tried to build glibc 1.6.0 on Centos6 so that I could use it by following,</p>
  
  <p><a href="https://unix.stackexchange.com/questions/176489/how-to-update-glibc-to-2-14-in-centos-6-5">https://unix.stackexchange.com/questions/176489/how-to-update-glibc-to-2-14-in-centos-6-5</a></p>
  
  <p>The steps worked, but it lead to this error trying to run Tensorflow, seems like it has a dependecy on another lib...</p>
  
  <blockquote>
    <p>error while loading shared libraries: __vdso_time: invalid mode for dlopen()</p>
  </blockquote>
  
  <p>At this point I'm ready to give up, and try installing Centos7, but this route will require we upgrade 12 production servers...</p>