Link

Java single file execution

Java 11 introduced Java Single File Execution (JEP-330) which enhanced the java launcher to run a program supplied as a single file of Java source code, including usage from within a script by means of “shebang” files and related techniques.

$ vi hello

The script file name does not need to match the class name.

#!/usr/bin/java --source 11

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

The script needs to be executable

$ chmod +x hello

and can be executed like any other script

$ ./hello

Hello World!!

The gist x_init shows a more elaborated example.