Meaningless Drivel is fun!*
The moose likes Java in General and the fly likes Unable to execute class file in command prompt. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Unable to execute class file in command prompt." Watch "Unable to execute class file in command prompt." New topic
Author

Unable to execute class file in command prompt.

Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
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

Joined: Jan 28, 2010
Posts: 35
check the class file inside Textpad folder.If it is not there then search it
Punya Pratap Singh
Ranch Hand

Joined: Nov 23, 2010
Posts: 80
I mentioned it that class file is created and it is in TextPad folder.
Murtaza Lokhandwala
Ranch Hand

Joined: Jan 28, 2010
Posts: 35
May be its an Internal error..u can reinstall jdk. I also faced the same prob!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Unable to execute class file in command prompt.