Search code examples
javaeclipseswingclasspathgraphstream

Unable to execute Java GUI from command like sporatically


I have a GUI application built with Swing and a 3rd party graphing library called GraphStream (there are 2 JARs to include). This is probably a classpath issue, and independent of the library itself.

When I run from Eclipse, the GUI appears and there are no errors/warnings.

However, when I attempt to run from the command line, I either receive a NoClassDefFoundError, or the application runs and "hangs" indefinitely while loading libraries. This appears to be totally sporadic. Sometimes the error prints, and sometimes the JVM just hangs. This is how I am running the application:

java -verbose -classpath '../lib;.' -cp . bt_sim <args>

The two JAR files are in the "../lib" directory (and those are the only two files in that directory). Moreover, the classes that I've written are in "." - When an error is actually raised, this is the output:

... Other loading output from JVM debug ...
[Loaded java.util.IdentityHashMap$KeySet from C:\Program Files\Java\jre8\lib\rt.jar]
java.lang.NoClassDefFoundError: org/graphstream/graph/Graph
    at bt_sim.main(bt_sim.java:70)
Caused by: java.lang.ClassNotFoundException: org.graphstream.graph.Graph
    at java.net.URLClassLoader$1.run(Unknown Source)

And when the application hangs:

...
[Loaded java.util.Formatter$Flags from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded java.util.Formatter$Conversion from C:\Program Files\Java\jre8\lib\rt.jar]
[Loaded sun.misc.Cleaner from C:\Program Files\Java\jre8\lib\rt.jar]

This same error is occurring even when I run from a Linux environment. In all cases, I'm running with JRE 8. In Eclipse, and when running from the command line, there are only TWO JAR files being included (the 3rd party graphing dependencies). The JAR files are in the "lib" directory in my command above. Any ideas on what to investigate would be super helpful!

MY SOLUTION I did not realize that I had to include each third-party JAR on the classpath explicitly. Once I did this, the application executed as-expected :-/


Solution

  • Note that -classpath and -cp are aliases:

    $ java -help
        …
        -cp <class search path of directories and zip/jar files>
        -classpath <class search path of directories and zip/jar files>
                      A : separated list of directories, JAR archives,
                      and ZIP archives to search for class files.
    

    I suspect that one overrides the other when used together. Instead, use the form that includes the required lib directory and path separator for your platform.