1

I was trying to manually comiple the HelloWorldSWT.java from the eclipse tutorial, so I used

$ javac -cp /opt/eclipse/plugins/org.eclipse.swt.gtk.linux.x86_*.jar HelloWorldSWT.java

to compile the program, and it worked. But when I tried to run it like this

$ java -cp /opt/eclipse/plugins/org.eclipse.swt.gtk.linux.x86_*.jar:. HelloWorldSWT

NoClassDefFoundError was thrown:

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Display
    at HelloWorldSWT.main(HelloWorldSWT.java:19)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Display
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    ... 1 more

I tried to add quotes to the path

$ java -cp "/opt/eclipse/plugins/org.eclipse.swt.gtk.linux.x86_*.jar:." HelloWorldSWT

but it didn't work, either.

Then I tried various combo of classpaths to get it run, I used the debug perspective of eclipse to see how it ran my program, it looked like this:

/usr/lib/jvm/jdk1.7.0_51/bin/java \
-agentlib:jdwp=transport=dt_socket,suspend=y,address=localhost:60185 \
-Djava.library.path=/home/joyeecheung/workspace/org.eclipse.swt.gtk.linux.x86 \
-Dfile.encoding=UTF-8 \
-classpath /home/joyeecheung/workspace/HelloWorldSWT/bin:/opt/eclipse/plugins/org.eclipse.swt_3.103.0.v20140605-2008.jar:/home/joyeecheung/workspace/org.eclipse.swt.gtk.linux.x86/bin \
HelloWorldSWT

OK, they didn't use the asterisks, so I tried

$ javac -cp /opt/eclipse/plugins/org.eclipse.swt.gtk.linux.x86_3.103.0.v20140605-2012.jar HelloWorldSWT.java
$ java -cp /opt/eclipse/plugins/org.eclipse.swt.gtk.linux.x86_3.103.0.v20140605-2012.jar:. HelloWorldSWT

to compile and run it, it worked.

So I'm wondering why when I was using the asterisk, Java seemed unable to find the class?

I'm using Ubuntu 12.04, and oracle jdk 1.7.0_51. I haven't added the current dir . into my $CLASSPATH environment variables. The HelloWorldSWT program looked like this:

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class HelloWorldSWT {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Display display = new Display();
        Shell shell = new Shell(display);
        shell.setText("Hello World!");
        shell.open();
        while(!shell.isDisposed()) {
            if (!display.readAndDispatch())
                display.sleep();
        }
        display.dispose();
    }

}
Joyee
  • 163
  • 8
  • possible duplicate of [Using wildcards in java classpath](http://stackoverflow.com/questions/10847850/using-wildcards-in-java-classpath) – Joe Jul 14 '14 at 14:25
  • And I wondered isn't it a common question...Why didn't it jump in my google results? Thanks for pointing out the answer! – Joyee Jul 14 '14 at 14:29

1 Answers1

1

From (http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html):

Understanding class path wildcards

Class path entries can contain the basename wildcard character , which is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR. For example, the class path entry foo/ specifies all JAR files in the directory named foo. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory.

In your case try java -cp /opt/eclipse/plugins/* instead of java -cp /opt/eclipse/plugins/org.eclipse.swt.gtk.linux.x86_*.jar

DavidPostill
  • 7,734
  • 9
  • 41
  • 60