• Post Reply Bookmark Topic Watch Topic
  • New Topic

The dreaded NoClassDefFoundError post  RSS feed

Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello again...

I have been playing around while reading chapter10 of the self study book for OCP SE6 and wouldn't you know I am having problems with NoClassDefFoundError!

read several of the posts on the board and they all seem to make sense I am wondering if it has to do with my environment setup.

I set up a simple class based on what I was reading here in another thread. You know the com\wickedlysmart\MyClass.java example.

pretty straight forward. package com.wickedlysmart is not off of root. It is a subdirectory off of my Dropbox. I have the following environment variables set up.

SRC = C:\Users\leonine\JavaCert\Dropbox\SRC\
BUILD = C:\Users\leonine\JavaCert\Dropbox\BUILD\
CLASSPATH = .;C:\Program Files\Java\jdk1.6.0_34;C:\Program Files\Java\jre6\bin;C:\Users\leonine\JavaCert\Dropbox;C:\Users\leonine\JavaCert\Dropbox\SRC;C:\Users\leonine\JavaCert\Dropbox\BUILD;C:\Program Files (x86)\Java\jre6\lib\ext\QTJava.zip;%SRC%;%BUILD%

so the full path would be


I was able to execute

javac -cp com/wickedlysmart -d %BUILD% com/wickedlysmart/MyClass.java ---from within the %SRC% directory

and it did generate the class file in the correct location. Interesting to note that I had to use the / to reference the directory instead of \ even though it is not unix machine.

Then I executed the following and they all failed except one.

from within %BUILD%

java -cp %BUILD% com/wickedlysmart/MyClass ---worked
java -cp %BUILD%/com/wickedlysmart com/wickedlysmart/MyClass ---failed
java -cp com/wickedlysmart com/wickedlysmart/MyClass ---failed

from within %BUILD%/com

java -cp wickedlysmart com/wickedlysmart/MyClass ---failed

from within %BUILD%/com/wickedlysmart

java -cp . com/wickedlysmart/MyClass ---failed

ALL produced the same error

Exception in thread "main" java.lang.NoClassDefFoundError: com/wickedlysmart/MyClass
Caused by: java.lang.ClassNotFoundException: com.wickedlysmart.MyClass
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)
Could not find the main class: com/wickedlysmart/MyClass. Program will exit.

I did try all of these commands using the \ instead as I read that in Windows it is different. Unfortunately no luck.

Conceptually it makes sense but when applied can make for some late nights.

Pressing on for the time being but any help is greatly appreciated.
Ranch Hand
Posts: 61
Java Linux Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Brian,

If I understood your question correctly you want to know why only one way worked and rest failed, if that is the case then I would like to clear two points:

(1) When a class is declare in a package then its name is changed to packagename.classname (in your case it will be com.wickedlysmart.MyClass)

(2) In order to run a class enclosed in a package , one has to be in the parent directory of the package(in your case the classpath should include %BUILD% path only), the reason is jvm looked for the hierarchy com/wickedlysmart/MyClass relative to the path you have mentioned in your classpath so if your classpath is set to %BUILD%/com/wickedlysmart than jvm will search for the class file in %BUILD%/com/wickedlysmart/com/wickedlysmart/MyClass which is a wrong path.

java -cp %BUILD% com/wickedlysmart/MyClass --- search class file %BUILD%/com/wickedlysmart/MyClass ---- right
java -cp %BUILD%/com/wickedlysmart com/wickedlysmart/MyClass ---> search class file under %BUILD%/com/wickedlysmart/com/wickedlysmart/MyClass -- wrong
java -cp com/wickedlysmart com/wickedlysmart/MyClass --- search class file under %BUILD%/com/wickedlysmart/com/wickedlysmart/MyClass -- wrong

So Bottomline is the correct way to execute a package enclosed class is

java -cp <path upto parent directory of package> packagename.classname

Forward slash (/) and backward slash (\) both works on Windows.

Brian Carlisle
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. Makes sense. I removed the package and found that I was able to get the program to run using the classpath references that were failing before but I also needed to remove the relative path reference before the actual class name.


java -cp . MyClass ---works with no package from com/wickedlysmart directory
java -cp wickedlysmart MyClass ---works with no package from com directory


Appreciate the response. Thanks.
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!