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

1

3

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?

Preoccupation answered 22/9, 2020 at 17:33 Comment(0)
A
13

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
Ambience answered 22/9, 2020 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.