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();
}
}