24 September 2018

Java 11 includes JEP 330 which allows to use Java source-files like shell-scripts.

Create a file named util with the following content:

#!java --source 11

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

Make sure it is executable by running chmod u+x util.

Running the script, will compile it on the fly:

> ./util Daniel
Hello Daniel!

As of now, editors like Visual Studio code don’t recognize the file as Java files automatically. This means, code-completion and syntax hightlighting do not work without manual steps. Let’s hope this gets fixed soon after the release of Java 11.