Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NoSuchClassFound exception

 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Ankit Garg
Sheriff
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ...
 
rohan yadav
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9510
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 156
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Understood, thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic