Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unable to execute class file in command prompt.

 
Punya Pratap Singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I was trying to excute a simple java program from command prompt.
For this I created a user varibale path and set its value as
C:\Program Files\Java\jdk1.5.0_22\bin.
also I put java file in C drive under TextPad folder.

when I comile the program by typing
C:\>javac TestPad/MyClass.java ----it compiles fine and creates MyClass.class in same folder(C\TextPad)
but when I run it by typing
C:\>java TestPad/MyClass ---------it gives the following error message.

Exception in thread "main" java.lang.NoClassDefFoundError: TextPad\MyClass (wron
g name: MyClass)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
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)
Could not find the main class: TextPad\MyClass. Program will exit.

why is it happening ,please help me out .
Thanks a lot.
 
Murtaza Lokhandwala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
check the class file inside Textpad folder.If it is not there then search it
 
Punya Pratap Singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mentioned it that class file is created and it is in TextPad folder.
 
Murtaza Lokhandwala
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be its an Internal error..u can reinstall jdk. I also faced the same prob!
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Punya,

Please try below -

java -cp /TestPad MyClass

where -cp specifies the class path
/TestPad - The folder where .class file (MyClass.class) resides


Since TestPad is not the package name of the MyClass java class you cannot run it TestPad.MyClass

If you make TestPad as the Package name for MyClass then use the command -> java TestPad.MyClass (Note that the compilation javac command need not be changed)

Thanks much!
John
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic