Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java 11 does not produce a .class file when compiling the class with 'java'

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?

like image 905
hellosr Avatar asked Apr 20 '26 05:04

hellosr


1 Answers

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
like image 95
Giorgi Tsiklauri Avatar answered Apr 21 '26 19:04

Giorgi Tsiklauri



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!