1

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.

7
  • 1
    missing information, what is your OS? what is the path of CLASSPATH? Commented Dec 6, 2018 at 16:40
  • does HelloWorld.class file generate after you run javac HelloWorld.java? Commented Dec 6, 2018 at 16:41
  • @Spara highly likely no. Commented Dec 6, 2018 at 16:42
  • Why are you passing -Xdiag when you are running the program? Commented Dec 6, 2018 at 16:52
  • @snr Why does the OS and CLASSPATH matter here? Commented Dec 6, 2018 at 16:52

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.