Have a look at JEP 330.
Since Java 11, java FileName.java
compiles and runs FileName.java
; however, compilation happens "behind the scenes", without explicitly producing corresponding .class
file. Instead, it directly loads corresponding byte-code into the JVM instance.
Motivation
Single-file programs (where the whole program fits in a single source file) - are common in the early stages of learning Java, and when writing small utility programs. In this context, it is pure ceremony to have to compile the program before running it. In addition, a single source file may compile to multiple class files, which adds packaging overhead to the simple goal of "run this program". It is desirable to be able to run the program directly from source with the java launcher:
java HelloWorld.java
If you want to have the .class
file as an output, you should still use javac
, as:
javac FileName.java