简体   繁体   中英

Error java executing: Couldn't find or load the main class

I'm trying to execute a simple java code (I have already compiled it with no problems) but it gives me the next error:

c:\\Users\\alejandro\\Desktop> java HelloWorld.java Error: Couldn't find or load the main class.

The code is the next:

public class HelloWorld{

    public static void main(String[] args){

        System.out.println("Hello world!");

     }
}
  • I have set the PATH variable correctly.
  • I have deleted CLASSPATH variable.
  • I have both files (.java and .class) in my Desktop.

You're specifying the name of the source file . That's not what you provide to the java command - you specify the class name .

java HelloWorld

This assumes that HelloWorld.class is somewhere on the classpath, which would default to "the current directory".

If you had a package, eg

package foo;

public class HelloWorld {
    ...
}

Then you would want to put HelloWorld.java in a directory called foo , and compile and run from the root directory:

> javac foo\HelloWorld.java
> java foo.HelloWorld

Note how now the fully-qualified class name is foo.HelloWorld , not foo\\HelloWorld .

when you run the compiled file, you should use only the class name. The compiled file will have an extension of .class but you should not add any extension. Just use the class name.

change

c:\Users\alejandro\Desktop> java HelloWorld.java

to

c:\Users\alejandro\Desktop> java HelloWorld

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM