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

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

 
saurabh nema
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

package com.jmd.sorav.temp;

public class HelloWorld{

public static void main(String[] args) {

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

}

}
 
saurabh nema
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 98
Eclipse IDE Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48642
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you so much to all of you.. i appreciate your quick response .. Now i can run this..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic