Issue
I have Java 11 installed. When I run the command java <filename.java>
, for compiling and running the code, it compiles and runs the program, shows the output in CMD, but it doesn't produce any .class
file.
Could you explain to me why it does not produce the .class
file?
Solution
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
Answered By - Giorgi Tsiklauri
Answer Checked By - Katrina (JavaFixing Volunteer)