File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 9321
    
  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: 9321
    
  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