When I had only HelloWorld.java
in the directory without HelloWorld.class
, java HelloWorld
in Windows cmd raised a class not found exception. However, java HelloWorld.java
was executed properly without making HelloWorld.class
.
From what I understand, JVM reads the bytecodes of a file with .class extension, but fails to read .java file. I understand that java.exe is just a launcher to create JVM and run it, but am now confused how it can work properly with java code, not byte code.
In modern Java, as a convenience to beginner Java students, the java app will graciously compile, and then run, a .java
file.
Think of it as java app detecting a source-code file, then sub-contracting the compilation work out to javac app, caching the compiled class in memory, and lastly continuing onwards to run the newly compiled class.
A JDK comes bundled with a few dozen tools. A couple of these are crucial:
.java
file of source code text into .class
file of bytecode..class
file by launching a JVM.The process steps are:
.java
file, including a main
method inside a class
definition..class
file.main
method by launching the java app bundled with every JDK.You can skip some steps.
.java
file. The java app will automatically compile first, as if javac were called on your behalf. Then the java app will go on to run the newly compiled class.
.class
file is written to storage. The compiled class is cached in memory.java HelloWorld.java
was executed properly without making HelloWorld.class.main
java
file without declaring a class. Just write the main
method alone, with nothing around it.public static
from your main
method declaration.The Java team is making concerted efforts to ease the way for beginning Java programmers, to smooth over the initial speed bumps. They are trying to mask some of the elaborate ceremony, and make the tooling more friendly and accommodating.
The underlying structures and features of Java are still in place. This is not a “dumbing-down” of Java. These efforts are just attempts at making reasonable accommodations for beginners.
Combining these features, getting started with a Hello World app is a simple as writing the following in a HelloWorld.java
file:
void main() {
System.out.println("Hello, World!");
}
… and then on the command-line calling:
java HelloWorld.java
Result on the console:
Hello, World!
That is a vast reduction in labor and confusion for a brand-new student of Java.
By the way… Another piece of making Java easier to use is jshell, a REPL for Java.
Both beginners and pros find jshell handy for running short bits of Java with immediate feedback.
Another useful tool for beginners is BlueJ, an IDE designed for students.