Simple HelloWorld java program

I created a sample java program in notepad and compile the code on dos prompt and I got the below error message

Error: Could not find or load main class HelloWorldApp.class
Caused by: java.lang.ClassNotFoundException: HelloWorldApp.class

But the same project I build on IntelliJ there was no issue, what is the reason

What commands are you executing at the command prompt which give that result? It sounds like you forgot to compile before running, but that is just one possibility.

It is a one-line code

/**

  • The HelloWorldApp class implements an application that
  • simply prints “Hello World!” to standard output.
    /
    import java.lang.
    ;
    class HelloWorldApp {
    public static void main(String args) {
    System.out.println(“Hello World!”); // Display the string.
    }
    }

File name save as “HelloWorldApp.java”

Compile: javac HelloWorldApp.java

java HelloWorldApp / java HelloWorldApp.class

Do you see the HelloWorldApp.class file after running the javac compile step? Is there any output after you run javac HelloWorldApp.java?

Also, are you using command prompt (cmd.exe) or PowerShell?

also, check your path, is it targetted to your current working file

I have created to 2 paths in an environment variable

System
CLASS_PATH = C:\Program Files\Java\jdk-19\lib*.jar
JAVA_HOME = C:\Program Files\Java\jdk-19
User and System
PATH = C:\Program Files\Java\jdk-19\bin

I don’t know how to target to a current working file

After I compile the .java it creates the .class file but the result

I am using command prompt (cmd.exe)

    /*
    The HelloWorldApp class implements an application that
    simply prints “Hello World!” to standard output.
    */

    class HelloWorldApp {
         public static void main(String[] args) {
              System.out.println(“Hello World!”); // Display the string.
         }
    }

I think the error in your code comes from the comment at the top, the comment does not close correctly

The import statement also looks broken/wrong. Just delete that line.

After removing the comments, I run the code java HelloWorld.java I got the result, but if I compile the code and run the code java HelloWorld or java HelloWorld.class it gives me the error. Below is the command line

Z:\COMP501\application>javac HelloWorldApp.java

Z:\COMP501\application>java HelloWorldApp.java
Hello World!

Z:\COMP501\application>java HelloWorldApp.class
Error: Could not find or load main class HelloWorldApp.class
Caused by: java.lang.ClassNotFoundException: HelloWorldApp.class

Z:\COMP501\application>java HelloWorldApp
Error: Could not find or load main class HelloWorldApp
Caused by: java.lang.ClassNotFoundException: HelloWorldApp

Z:\COMP501\application>java HelloWorldApp.java
Hello World!

Java Version
Z:\COMP501\application>java --version
java 19 2022-09-20
Java™ SE Runtime Environment (build 19+36-2238)
Java HotSpot™ 64-Bit Server VM (build 19+36-2238, mixed mode, sharing)

Can you print out whatever is in that directory using the dir command:

dir

Z:\COMP501\application>dir
Volume in drive Z is MyAU
Volume Serial Number is E9BD-CB91

Directory of Z:\COMP501\application

2022-09-25 07:00 AM .
2022-09-25 06:58 AM …
2022-09-25 07:02 AM 432 HelloWorldApp.class
2022-09-25 06:59 AM 166 HelloWorldApp.java
2 File(s) 598 bytes
2 Dir(s) 3,128,951,115,776 bytes free

of course. you will get an error, cause you call compiled javac with the extension “.class”.
after compiling, just run java HelloWorldApp

This sequence of events has got me scratching my head. The first one looks correct for compiling the class (and should produce the HelloWorldApp.class file with the Java bytecode). The second one in that sequence should not work, but does for some strange reason. The third one is not supposed to work (and does not, so that is working as intended). The last one is what should work for running the code in the class file and the fact that it does not is very surprising and confusing.

As you said, i delete .class file and recompile and still the same

Z:\COMP501\application>del *.class

Z:\COMP501\application>dir
Volume in drive Z is MyAU
Volume Serial Number is E9BD-CB91

Directory of Z:\COMP501\application

2022-09-25 01:11 PM .
2022-09-25 07:46 AM …
2022-09-25 06:59 AM 166 HelloWorldApp.java
1 File(s) 166 bytes
2 Dir(s) 3,128,099,672,064 bytes free

Z:\COMP501\application>javac HelloWorldApp.java

Z:\COMP501\application>dir
Volume in drive Z is MyAU
Volume Serial Number is E9BD-CB91

Directory of Z:\COMP501\application

2022-09-25 01:11 PM .
2022-09-25 07:46 AM …
2022-09-25 01:11 PM 432 HelloWorldApp.class
2022-09-25 06:59 AM 166 HelloWorldApp.java
2 File(s) 598 bytes
2 Dir(s) 3,128,099,672,064 bytes free

Z:\COMP501\application>java HelloWorldApp
Error: Could not find or load main class HelloWorldApp
Caused by: java.lang.ClassNotFoundException: HelloWorldApp

I got it fixed, I removed the CLASSPATH from the environment variable.

Z:\COMP501\application>dir
Volume in drive Z is MyAU
Volume Serial Number is E9BD-CB91

Directory of Z:\COMP501\application

2022-09-25 01:25 PM .
2022-09-25 07:46 AM …
2022-09-25 06:59 AM 166 HelloWorldApp.java

Z:\COMP501\application>javac HelloWorldApp.java

Z:\COMP501\application>dir
Volume in drive Z is MyAU
Volume Serial Number is E9BD-CB91

Directory of Z:\COMP501\application

2022-09-25 01:25 PM .
2022-09-25 07:46 AM …
2022-09-25 01:25 PM 432 HelloWorldApp.class
2022-09-25 06:59 AM 166 HelloWorldApp.java

Z:\COMP501\application>java HelloWorldApp
Hello World!

Z:\COMP501\application>

1 Like