Java — Recent Developments
Scripting & Shell Introduction
In the last article on “Modules & Leaner Runtime”, I had explored Java Platform Modules System (JPMS) and it’s aim of creating leaner runtime.
In this article, let us see some of the scripting features introduced in Java in recent versions.
Another notable feature introduced in recent versions of Java is scripting support. It is now easier to try out Java features & syntax quickly without having to create a new Java project. Similarly, one can write scripts using Java language.
The main advantage of the below features is that they give a good starting point for developers learning Java; these are sure to give a perception of ease and simplicity regarding the language.
JShell
JShell was introduced in Java 9. It allows programmers & learners to quickly try out a piece of code in the shell. Similar to Python & PHP Shell, this can be used if we want to try out new language syntax / features and also to verify a logic quickly.
In the above shell snippet, those highlighted in green are the code snippets / commands entered and the output are highlighted in red.
Single File Java Program Execution without ‘javac’
Another cool trick introduced in Java 11 is that when we have a single file Java program, we do not have to compile and then run it. We can directly do the below:
java MySingleFileApp.java
There are a few changes to java file conventions. But beware; these apply only when we are “running java files directly”. When we compile and run, the usual rules apply.
- The file need not be suffixed with .java. We can simply use the below command to execute files which have java code. Notice the — source option. This is important when a file does not have a .java suffix.
java — source <use any version since 11> MySingleFileApp
- The first class in the file has to contain the main method.
- There can be any number of classes & public classes in a file.
- File name need not match the main class name.
Java as Shell Script
Putting the above two features together, it is now possible to write shell scripts using Java language. One can write a script like below, name it print-date:
#!/usr/bin/java --source 11
import java.time.LocalDateTime;
public class PrintDate {
public static void main(String [] args) {
System.out.println(LocalDateTime.now());
}
}
And execute using a command as below:
chmod 755 print-date
./print-date
In this series of articles, we looked at some of the major changes to the Java language in recent years. Many of these are aimed at making it more developer friendly and also to incorporate many features to bring it inline with other latest programming languages.
When combined with libraries and frameworks like Spring, Java is still an easy-to-develop programming language with support for almost any feature required for developing most kinds of applications, very good community support and documentation.
For those concerned about longer boot times and heavier runtime image size while developing microservices, frameworks like Micronaut, Quarkus etc. will provide blazing fast startup times and a small runtime footprint.