Hi All

I was reading through the swing tutorial at http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html
and i copied and pasted the provided source code for the HelloWorldSwing.java program.
It compiled fine but when i went to run it the following error message was returned

java HelloWorldSwing
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldSwing (wrong name: start/HelloWorldSwing)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

I have no idea what the error means, like i said i copied the source code from the sun site so i thought it would run ok so most likely there is something wrong with my pc.

Does anyone know whats wrong please?

Many thanks

HLA91

Well, actually those directions aren't quite correct on the tutorial page. The file they reference has a package declaration

package start;

but they do not mention that and the command to run that file won't work as they have it written. Remove that package declaration and re-compile it and it should work fine.

If you leave in the package statement, you would need to have that class file in a directory called 'start' and from it's parent directory you would execute it as follows

java start.HelloWorldSwing
commented: Thanx for the tip :) +9

OK thanks, i removed the package declaration and it was all good, thanks for the info, strange that the tutorial didnt mention anything about it though and I would have expected them to, if it was going to affect the compilation

I suspect it was merely an oversight and needs to be corrected on that page.

its a classpath error check your classpath.

its a classpath error check your classpath.

No, it is not a classpath problem. It's a package declaration problem as already stated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.