954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

java.lang.NoClassDefFoundError: ???

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

HLA91
Junior Poster
177 posts since Oct 2006
Reputation Points: 7
Solved Threads: 3
 

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
Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

HLA91
Junior Poster
177 posts since Oct 2006
Reputation Points: 7
Solved Threads: 3
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

its a classpath error check your classpath.

tactfulsaint
Junior Poster in Training
78 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 
its a classpath error check your classpath.


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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You