File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Not able to Run a class file containing a package using command prompt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Not able to Run a class file containing a package using command prompt" Watch "Not able to Run a class file containing a package using command prompt" New topic
Author

Not able to Run a class file containing a package using command prompt

saurabh nema
Greenhorn

Joined: Aug 24, 2013
Posts: 6

package com.jmd.sorav.temp;

public class HelloWorld{

public static void main(String[] args) {

System.out.println("Hello World ..");

}

}
saurabh nema
Greenhorn

Joined: Aug 24, 2013
Posts: 6
Hi Guys,

I can compile this code successfully but when i am going to run it it shows this error :-


C:\sorav>javac HelloWorld.java

C:\sorav>java HelloWorld
Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong nam
e: com/jmd/sorav/temp/HelloWorld)
at java.lang.ClassLoader.defineClass1(Native Method)
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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)

Please help me friends , Thanks in advance..
Stuie Clarky
Ranch Hand

Joined: Nov 09, 2012
Posts: 72

The package needs to match the set of folders that the .java file lives in.

So you java file needs to be found in com/jmd/sorav/temp/ which based on the command prompt you linked, is not the case.

Hope this helps

S


OCAJP 7
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Also note that you need to specify the fully qualified name when running the class
i.e.
java com.jmd.sorav.temp.HelloWorld
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
What Stuie Clarky means is, you have to navigate to .. several times until the location of that class corresponds to its package name, so you could find it by writing com/jmd/sorav/temp/HelloWorld
There are several ways to compile classes with package names; you will find several links in this old post.
saurabh nema
Greenhorn

Joined: Aug 24, 2013
Posts: 6
Thank you so much to all of you.. i appreciate your quick response .. Now i can run this..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Not able to Run a class file containing a package using command prompt
 
Similar Threads
Compiling class with package
package location
Access a class no package
Access modifier confusion
How to access