Shebang Mechanism Support in IntelliJ IDEA 2020.3

The upcoming IntelliJ IDEA 2020.3 will support the shebang mechanism to run single-file programs directly from source. This enhancement to the Java launcher was delivered in JDK version 11.

The idea of the shebang mechanism is to reduce packaging overhead and simply run the program.
To execute the file with the operating system’s shebang mechanism, its first line should start with #!.

Here’s how to make it work in IntelliJ IDEA:

  1. Create a simple file without an extension. IntelliJ IDEA can now postpone the file type detection, which was not possible in previous versions. Note that the file may be placed inside or outside of the source root.
  2. After the shebang character sequence (#!) define the path to the JDK. You can use the –source option to specify the source version of the source code or the default version of the platform will be used. Other parameters like classpath can be defined as well.
  3. Enter the source code starting from the second line
    #!/Users/Olga.Klisho/Library/Java/JavaVirtualMachines/corretto-11.0.7/Contents/Home/bin/java --source 11
    public class HelloJavaScripts {
    public static void main(String[] args) {
        System.out.println("Shebang file sample");
  4. The resulting file should be made executable: you can use the chmod +x filename command in the Terminal to execute it as a program.

Now the file is ready to use. IntelliJ IDEA will highlight it as a common Java file. Code completion, inspections, code generation, and refactoring all work here as well.

Try the new features coming in IntelliJ IDEA 2020.3 for free through our EAP program!

image description