Java 11 – “javac” not needed, run single class programs directly with “java”

Before Java 11

With Java as we know, for single class programs, once we create a Java file we compile it first & then run it.

For ex. HelloWorld.java

To execute this program,

  1. First we compile java file using “javac”
  2. Then we run generated java class using “java”

With Java 11

With Java 11, as part of JEP 330: Launch Single-File Source-Code Programs , its not necessary to run “javac”. Single class java source file can directly be executed using “java”

 

Notice that in earlier java 10 example, we gave class name HelloWorld to “java” with. In Java 11 example, we have given file name HelloWorld.java to “java”. When java file name is given to java command, it is called “source-file mode”.

As per openjdk,

In source-file mode, the effect is as if the source file is compiled into memory, and the first class found in the source file is executed

 

Leave a Reply

Your email address will not be published.