简体   繁体   中英

Exception in thread “main” java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64

I am trying setup TensorFlow Java application in Eclipse Oxygen (OS: Ubuntu Linux 16.x). I installed Tensorflow and followed the process mentioned in official documentation for Java (Maven Project) installation. I downloaded libtensorflow-1.3.0.jar, jni files and included in the build path. When I execute the program I get this following error

Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: linux, architecture: x86_64. See https://github.com/tensorflow/tensorflow/tree/master/tensorflow/java/README.md 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.
at org.tensorflow.NativeLibrary.load(NativeLibrary.java:66)
at org.tensorflow.TensorFlow.init(TensorFlow.java:36)
at org.tensorflow.TensorFlow.<clinit>(TensorFlow.java:40)
at com.tensorflow.malwaredetection.App.main(App.java:13)

App.java

package com.tensorflow.malwaredetection;

import org.tensorflow.TensorFlow;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        System.out.println( "Hello World!"+ TensorFlow.version() );
    }
}

POM.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>TensorFlow</groupId>
  <artifactId>MalwareDetection</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>MalwareDetection</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
     <exec.mainClass>App</exec.mainClass>
       <!-- The sample code requires at least JDK 1.7. -->
       <!-- The maven compiler plugin defaults to a lower version -->
       <maven.compiler.source>1.8</maven.compiler.source>
       <maven.compiler.target>1.8</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

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


  </dependencies>
</project>

I got tired of this error and tried to do this in an old-fashioned way. Created App.java in a separate folder and included jar, jni files in the same directory. When I execute this from command line, I get different error

dev@ubuntu:~/Downloads$ javac -cp libtensorflow-1.3.0.jar Test1.java
dev@ubuntu:~/Downloads$ java -cp libtensorflow-1.3.0.jar:. -Djava.library.path=./jni Test1
Error: Could not find or load main class Test1
dev@ubuntu:~/Downloads$ 

I think you need to include jni library dependency in your pom.

<dependency>
    <groupId>org.tensorflow</groupId>
    <artifactId>libtensorflow_jni</artifactId>
    <version>1.1.0</version>
</dependency>

When you have tensor flow dependency in your pom.xml file as below

<dependencies>
<dependency>
  <groupId>org.tensorflow</groupId>
  <artifactId>tensorflow</artifactId>
  <version>1.14.0</version>
</dependency>

It will download required 3 libraries tensorflow-1.14.0.jar libtensorflow-1.14.0.jar libtensorflow_jni-1.14.0.jar

So you have to manually extract the 3rd jar(libtensorflow_jni) and get your OS compatible file like for windows you have to copy the tensorflow_jni.dll file and paste in your project root directory that will solve your problem.

For Linking error the JNI library which you have downloaded is

  1. not compatible with OS you are running,
  2. is not in PATH () or
  3. with lesser probability TensorFlow bu which you should be able to confirm from TF triage.

For second could not find class

  1. your PATH must be appended with "." so that it can find class in current folder from where it is running
  2. Less probability is if class name/package is not same.

If you can post code for class will check

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM