szymmy
szymmy

Reputation: 1

NoClassDefFoundError in HelloWorld program

I am trying to run my first java class but I always get this error from the command java HelloWorld

Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorld (wrong nam
e: helloworld/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$100(Unknown Source)
        at java.net.URLClassLoader$1.run(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 sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

I tried also with those commands and same error:

java -cp . HelloWorld

java -classpath . HelloWorld

Could you help me please solve my problem? Thanks

my code:

package helloworld;

/**
 *
 * @author Szymon
 */
public class HelloWorld {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

}

Upvotes: 0

Views: 71

Answers (1)

abacabadabacaba
abacabadabacaba

Reputation: 2692

You need to use fully qualified class name:

java -cp . helloworld.HelloWorld

Also, your current directory (or whatever the directory you specify with -cp) must be one level above the directory with HelloWorld.class.

Upvotes: 1

Related Questions