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
1 2 3 4 5 |
public class HelloWorld{ public static void main(String... args){ System.out.println("Hello Its All Binary !"); } } |
To execute this program,
- First we compile java file using “javac”
- Then we run generated java class using “java”
1 2 3 |
> "C:\Program Files\Java\jdk-10.0.2\bin\javac" HelloWorld.java >"C:\Program Files\Java\jdk-10.0.2\bin\java" HelloWorld Hello Its All Binary ! |
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”
1 2 |
>"C:\Program Files\Java\jdk-11.0.1\bin\java" HelloWorld.java Hello Its All Binary ! |
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