I'm currently trying to run my first java
script:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
I decided i'd take a little look into Java. However I come from languages like JavaScript and PHP which don't require any compiling or anything as such.
So far, I think i'm compiling it correctly in the command prompt:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\javac.exe" "HelloWorld.java"
It adds a file called: HelloWorld.class
so figured I did something right.
However, now when I try to actually run the program using:
C:\Users\Shawn>"C:\Program Files\Java\jdk1.7.0_25\bin\java.exe" "C:\Users\Shawn\HelloWorld.class"
I get this, Error: Could not find or load main class C:\Users\Shawn\HelloWorld.class
.
However, if I try that same command but use javac.exe
instead I get:
javac: invalid flag: C:\Users\Shawn\HelloWorld.class
Usage: javac <options> <source files>
use -help for a list of possible options
Why is this happening? Why isn't my program executing correctly?
The java
command takes the name of the class, not the name of the file.
It then uses the Java class loader to find the .class
file for that class in the current directory or the class path.
When you pass HelloWorld.class
, it looks for a class named class
in the package HelloWorld.
(that would be ./HelloWorld/class.class
)
You need to pass HelloWorld
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With