Mdp11
Mdp11

Reputation: 572

ClassNotFoundException on simple HelloWorld

I wrote this simple Hello World program:

public class HelloWorld {

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

which is named "HelloWorld.java". Then I compile it from the cmd using: "javac HelloWorld.java" and obtain the class file. Once I run the command "java -Xdiag HelloWorld" I get the following error:

Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
java.lang.ClassNotFoundException: HelloWorld
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398)
    at java.base/sun.launcher.LauncherHelper.loadMainClass(LauncherHelper.java:760)
    at java.base/sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:655)

Does anyone know why this is happening? The OS is W10 and I installed the jdk version 11.0.1.
Thank you.

Upvotes: 1

Views: 797

Answers (2)

Nayeem Shiddiki Abir
Nayeem Shiddiki Abir

Reputation: 27

Create your java class under a package name throw src folder.. I hope error will be solved

Upvotes: -1

Mdp11
Mdp11

Reputation: 572

Solved by modifying the CLASSPATH environmental variable, inserting "." in it, so that java searches for classes in the current working directory.

Upvotes: 1

Related Questions