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!");
}
}
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.