This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NoSuchClassFound exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NoSuchClassFound exception" Watch "NoSuchClassFound exception" New topic
Author

NoSuchClassFound exception

rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Hi,

my directory structure is following:
myproject
|
|------sources
| |
| |-------Test1.java
|
|
|------classes
| |
|-------

currently i am in myproject directory

When i compile program using javac it compiles fine, but when i run program using java command it gives Exception in thread "main" java.lang.NoClassDefFoundError: sources\Test1 error.

I have typed following commands in command prompt:

C:\myproject>javac sources\Test1.java

C:\myproject>java sources\Test1
Exception in thread "main" java.lang.NoClassDefFoundError: sources\Test1

C:\myproject>

But when i run same program from eclipse it shows the output.

can anyone please tell me whats the problem?


Sage of The Monstrous Toad of Mount Myoboku
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

you'll have to run the java command either from the sources directory OR use the java command from myproject directory with -cp sources flag i.e. java -cp sources Test1 ...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Oh its running now, that means i have to specify classpath while running program from other directory using java, am i right??
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

Yes, when you use the java command, the last parameter has to be the name of the class, it cannot contain paths like folder\ClassName . If your class is in a package like package1, then the java command has to be package1.ClassName not package1\ClassName...
rohan yadav
Ranch Hand

Joined: Oct 13, 2009
Posts: 156
Understood, thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NoSuchClassFound exception
 
Similar Threads
Error when running packaged code
Classpath issue
Query on Source File name
Getting error while running java program
Start: Applet not initialized