You can run Java source code without compiling ! Bye Bye javac !!!

Vicky AV
Nov 11, 2020

--

Image from Google Search

Java 11 came up with a cool feature to execute source code in single file without compiling it pre-hand

Lets assume Vicky.java file contains following beautiful world changing source code

class Vicky {

public static void main(String[] args) {
System.out.println("Hello World!");
}
}

For ages, we do the following

> javac Vicky.java (creates Vicky.class file)> java Vicky"Hello World !"

But from Java 11, its further simplified like below

> java Vicky.java"Hello World !"

So how it works ?

  • From Java 11, java command can compile as well as execute the Java source file
  • Remember, no .class file will be created while running java Vicky.java

Ok what if there is more than one class defined in same Vicky.java file ?

class Kee {  public static void main(String[] args) {
System.out.println("From Kee class");
}
}
public class Vicky {

public static void main(String[] args) {
System.out.println("From Vicky class");
}
}
> java Vicky.java"From Kee class"
  • java command executes the main method from the 1st class
  • public keyword in class Vicky proved insignificant in this case

Though these are small features, I feel its right step towards simplifying Java…

Thanks for reading..

--

--