Results 1 to 16 of 16
  1. #1
    ki_ha1984 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    118
    Rep Power
    0

    Default Error: Could not find or load main class + when i use tensorflow library

    I have the following code which is the sample code from the TensorFlow website, to test if it is running.
    Java Code:
    package javatensorflow;
    import org.tensorflow.Graph;
    import org.tensorflow.Session;
    import org.tensorflow.Tensor;
    import org.tensorflow.TensorFlow;
    public class JavaTensorFlow {
    
        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"));
          }
        }
      }
    }
    This image shows the error that i got when i run the project is this
    Capture222.PNG

    This is the configuration that i made based on the instruction of Tensorflow. To run the example through NetBeans (VM Options)
    Error: Could not find or load main class + when i use tensorflow library-capture1111.png

    Any help?

    Why returns this error:
    Java Code:
    Error: Could not find or load main class JavaTensorFlow
    C:\Users\username\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
    BUILD FAILED (total time: 0 seconds)
    Attached Thumbnails Attached Thumbnails Error: Could not find or load main class + when i use tensorflow library-capture222.png  

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Moved to Netbeans section - Looks like a Netbeans setup problem.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ki_ha1984 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    118
    Rep Power
    0

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Dear @Norm is everything ok, in my setup?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Sorry, I don't know how to use netbeans.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    ki_ha1984 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    118
    Rep Power
    0

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    i think that this is not netbeans issue because i compiled it manually and get the same error.

    I put the these files (HelloTF.java, libtensorflow-1.3.0.jar and tensorflow_jni.dll) in a folder.

    I complied the HelloTF.java like this
    Java Code:
    javac -cp libtensorflow-1.3.0.jar HelloTF.java
    Then i run it like this
    Java Code:
    java -cp libtensorflow-1.3.0.jar;. -Djava.library.path=jni HelloTF.class
    and i get this error
    Java Code:
    Error: Could not find or load main class HelloTF.class
    I think that the issues is something else, not the netbean.

    Any idea???

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Could not find or load main class HelloTF.class
    That is the name of the file: HelloTF.class
    The java command takes the name of the class: HelloTF

    Try it again without the .class extension
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    ki_ha1984 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    118
    Rep Power
    0

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    without the extension o get this error

    Java Code:
    Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: windows, 
    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 org.tensorflow.Graph.<clinit>(Graph.java:194)
            at HelloTF.main(HelloTF.java:8)
    a post again the code to see the 8th line
    Java Code:
    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"));
          }
        }
      }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    without the extension o get this error
    Ok, that says you are executing the java program and now have a problem finding the files it needs.

    java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: w
    Is there a native library needed for that program? Like a dll on Windows.

    Make sure the java program can find the native library.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    ki_ha1984 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    118
    Rep Power
    0

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Yes there is the tensorflow_jni.dll. Which is in the same folder with the HelloTF.java and HelloTF.java.
    And i executed like these

    Java Code:
    java -cp libtensorflow-1.3.0.jar;. -Djava.library.path=tensorflow_jni HelloTF
    and
    Java Code:
    java -cp libtensorflow-1.3.0.jar;. -Djava.library.path=tensorflow_jni[B].dll[/B] HelloTF
    and (the next is provides by the TensorFlow Instractions)
    Java Code:
    java -cp libtensorflow-1.3.0.jar;. -Djava.library.path=jni HelloTF
    and
    Java Code:
    java -cp libtensorflow-1.3.0.jar;. -Djava.library.path=./tensorflow_jni HelloTF
    and
    Java Code:
    java -cp libtensorflow-1.3.0.jar;. -Djava.library.path=C:\Users\username\Desktop\TensoFlow4Java\tensorflow_jni HelloTF
    and
    Java Code:
    java -cp libtensorflow-1.3.0.jar;. -Djava.library.path=C:\Users\chairik\Desktop\TensoFlow4Java\tensorflow_jni.dll HelloTF

    all the above give the same result

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Have you tried putting the dll in the same folder where you execute the java command?
    Or in a folder on the PATH environment variable?
    If you don't understand my response, don't ignore it, ask a question.

  11. #11
    ki_ha1984 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    118
    Rep Power
    0

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Hi Norm,

    Yes already i have the dll in the same folder with the HelloTF.java and HelloTF.class and libtensorflow-1.3.0.jar

    When you say
    Or in a folder on the PATH environment variable?
    , what exactly you mean ?

  12. #12
    ki_ha1984 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    118
    Rep Power
    0

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    I just tried the same project in a MacOS an got the same error when i execute this command
    Java Code:
    java -cp libtensorflow-1.3.0.jar:. -Djava.library.path=./jni HelloTF
    when i insert the ".:" before the library it worked, i mean i run this command

    Java Code:
    java -cp .:libtensorflow-1.3.0.jar:. -Djava.library.path=./jni HelloTF
    in windows i tried the same by inserting the ".;" before the library, but the problem did not solved.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Please use the dir command to display the contents of the folder where the java command is entered
    and then execute the java command.
    Copy the full contents of the command prompt window and paste it here.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    what exactly you mean ?
    On Windows the PATH environment variable has paths to folders so the OS can find commands that are entered at the command prompt.
    Last edited by Norm; 08-22-2017 at 01:46 PM.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    ki_ha1984 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    118
    Rep Power
    0

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Java Code:
    C:\JavaTFDemo>dir
     Volume in drive C has no label.
     Volume Serial Number is F250-182E
    
     Directory of C:\JavaTFDemo
    
    22/08/2017  02:09 μμ    <DIR>          .
    22/08/2017  02:09 μμ    <DIR>          ..
    20/08/2017  08:27 μμ               972 HelloTF.java
    20/08/2017  04:28 μμ            29.527 libtensorflow-1.3.0.jar
    15/08/2017  09:16 μμ        44.310.528 tensorflow_jni.dll
                   3 File(s)     44.341.027 bytes
                   2 Dir(s)  671.017.885.696 bytes free
    
    C:\JavaTFDemo>javac -cp libtensorflow-1.3.0.jar HelloTF.java
    
    C:\JavaTFDemo>java -cp libtensorflow-1.3.0.jar;. -Djava.library.path=jni HelloTF
    Exception in thread "main" java.lang.UnsatisfiedLinkError: Cannot find TensorFlow native library for OS: windows, 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 org.tensorflow.Graph.<clinit>(Graph.java:194)
            at HelloTF.main(HelloTF.java:8)
    
    C:\JavaTFDemo>
    Now i understand what you mean Path environment variables, here is the java paths that i use
    C:\Program Files\Java\jdk1.8.0_131\bin
    C:\Program Files (x86)\Java\jdk1.7.0_55\bin

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,904
    Rep Power
    30

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    Where is the HelloTF.class file?
    Try issuing the java command without the -Djava.library.path=jni option
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    ki_ha1984 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    118
    Rep Power
    0

    Default Re: Error: Could not find or load main class + when i use tensorflow library

    @Norm you are the best.

    Without the "-Djava.library.path=jni" option finally worked. When i say worked returns the simple message "Hello from 1.3.0" that was expected. Dont know if all the library will work perfectly without that command. But i will inform you soon.

    @Norm thank you again.

Similar Threads

  1. error, could not find or load main class!!!
    By Tejeshwar in forum New To Java
    Replies: 10
    Last Post: 04-11-2016, 02:48 PM
  2. Error: Could not find or load main class
    By Georgie23 in forum Eclipse
    Replies: 32
    Last Post: 10-30-2014, 11:44 AM
  3. Error: Could not find or load main class
    By iSamir in forum New To Java
    Replies: 16
    Last Post: 06-25-2013, 06:39 AM
  4. Error: could not find or load main class
    By sloppygecko in forum New To Java
    Replies: 3
    Last Post: 08-08-2012, 04:11 PM
  5. Replies: 3
    Last Post: 12-09-2011, 04:51 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •