Jagdish Talele
Jagdish Talele

Reputation: 73

ClassNotFound exception in java command

I have simple Hello word program. Program will compile and run when not declare namespace in code but when I declared the class within namespace and compile the program it will complie successfully but it will given an error classnotfound when this program run. My question is why java complier not availble to find class when declare namespace (package) in code?

Please find below source code:

package org;

public class Chunk
{

   public static void main(String [] args)
   {
    System.out.println("Hello, World");
   }
} 

and command is

java org.Chunk

and error is

java.lang.NoClassDefFoundError: org/Chunk
Caused by: java.lang.ClassNotFoundException: org.Chunk
        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: org.Chunk.  Program will exit.
Exception in thread "main"

Upvotes: 1

Views: 405

Answers (1)

dan
dan

Reputation: 13262

You are getting this error probably because the class is not part of the classpath. You can specify a classpath using -cp java option, to point to the directory/jar where the org.Chunk class is found.

Upvotes: 1

Related Questions