Run Java Source as Shell Scripts

JEP-330: "Launch Single-File Source-Code Programs" allows direct execution of Java source files without compilation. Now, you can also use Java as an interpreter program (instead of e.g. bash or zsh).

To execute the main method you will have to declare a specific shebang first (#!/[PATH]/bin/java --source [VERSION]), then a java class with a main method:


#!/usr/bin/java --source 11
public class App{
    public static void main(String ...args){
        System.out.println("hello, java (like script)");
    }
}

The entire script can be now located, in an executable file with an arbitrary name like e.g.: hellojavascript.sh.

Executing: hellojavascript.sh generates the following output: hello, java (like script)

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License