Home » Java » Core Java » Java 11: Running single-file programs and “shebang” scripts

About Fahd Shariff

Fahd Shariff
Fahd is a software engineer working in the financial services industry. He is passionate about technology and specializes in Java application development in distributed environments.

Java 11: Running single-file programs and “shebang” scripts

In Java 11, the java launcher has been enhanced to run single-file source code programs directly, without having to compile them first.

For example, consider the following class that simply adds its arguments:

import java.util.*;
public class Add {
  public static void main(String[] args) {
    System.out.println(Arrays.stream(args)
      .mapToInt(Integer::parseInt)
      .sum());
  }
}

In previous versions of Java, you would first have to compile the source file and then run it as follows:

$ javac Add.java
$ java Add 1 2 3
6

In Java 11, there is no need to compile the file! You can run it directly as follows:

$ java Add.java 1 2 3
6

It’s not even necessary to have the “.java” extension on your file. You can call the file whatever you like but, if the file does not have the “.java” extension, you need to specify the --source option in order to tell the java launcher to use source-file mode. In the example below, I have renamed my file to MyJava.code and run it with --source 11:

$ java --source 11 MyJava.code 1 2 3
6

It gets even better! It is also possible to run a Java program directly on Unix-based systems using the shebang (#!) mechanism.

For example, you can take the code from Add.java and put it in a file called add, with the shebang at the start of the file, as shown below:

#!/path/to/java --source 11
import java.util.*;
public class Add {
  public static void main(String[] args) {
    System.out.println(Arrays.stream(args)
      .mapToInt(Integer::parseInt)
      .sum());
  }
}

Mark the file as executable using chmod and run it as follows:

$ chmod +x add
$ ./add 1 2 3
6
Published on Java Code Geeks with permission by Fahd Shariff, partner at our JCG program. See the original article here: Java 11: Running single-file programs and “shebang” scripts

Opinions expressed by Java Code Geeks contributors are their own.

(0 rating, 0 votes)
You need to be a registered member to rate this.
1 Comment Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
chaitra Recent comment authors

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
newest oldest most voted
Notify of
chaitra
Guest

Hey thanks for sharing the valuable information. it is really helpful for us, I need some help for my Website