Jave Error: Could not find or load main class

I tried to run a small java application, and it failed a few times. At last, it runs correctly. I wrote it down in case I forgot it. 

File name: HelloWorld.java

The content of the file HelloWorld.java:

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

I saved the file in the path D:\netbeans\HelloWorld.

Then I typed after D:\: javac HelloWorld.java
It produced a file: HelloWorld.class
Then I typed: java HelloWorld

It showed:

Could not find or load main class d:\netbeans\HelloWorld

I wondered if the system environment is correct. I went to configure the system variables.
Right-click the computer, then click “change settings”, than at “Advanced” tab, click “Environment Variables…”. At the pop-up window, you can configure “system variables”.

The three system variables need to be configured:

1) JAVA_HOME

For example: D:\Program Files\Java\jdk1.7.0

2) CLASSPATH

.;%JAVA_HOME%\lib;

3) Path

%JAVA_HOME%\bin — here is the jdk’s path in my computer

Then test if it’s configured correctly:

D:\>java -version

It shows:
java version “1.7.0”
Java<TM> SE Runtime Environment <build 1.7.0-b147>
Java HotSpot<TM> Client VM <build 21.0-b17, mixed mode, sharing>

It’s good.

At “D:”, I tried to javac D:\netbeans\HelloWorld, java D:\netbeans\HelloWorld again, but it still shows: Could not find or load main class.

I entered to the path “D:\netbeans\”, where the HelloWorld.class is located.

Typed: java HelloWorld
It shows: Hello World!!

It works.

6 thoughts on “Jave Error: Could not find or load main class

  1. Mirta - Recipes Cookbook

    Second part of this tutorial, the stupid one:

    1. I had the same exact problem you find.
    2. I followed all your detailed steps without any results. It continue saying: “Could not find or load main class X.class”
    3. Broke my head against the wall
    4. Repeated step 3
    5. Finally I realized I was calling the file as “java X.class”, I removed the ext and typed “java X” and it works!

    Just in case you guys have the same problem, you can skip steps 3 and 4 😉

    Reply

Leave a Reply to Mirta - Recipes Cookbook Cancel reply

Your email address will not be published. Required fields are marked *