Scripting with Java - Sip of Java

Recent changes to the JDK and some long-existing APIs have made Java a more practical choice for writing small utility applications. This can benefit Java developers who may not have as much familiarity with scripting languages or prefer to use a consistent programming language. Let’s take a look at how to use Java for writing scripts.

Single-File Source-Code Application

JEP 330, introduced in JDK 11, was an update to the java launcher that allows it to directly launch a Single-File Source-Code application. Developers are no longer required to use javac to compile the code before running their application as the java launcher will compile the source code under the covers and launch the application. The java launcher can also transparently handle a source file with multiple classes. This improves the experience of writing and using utility applications written in Java.

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

Command

$ java HelloWorld.java

Output

Hello World

Shebang Files

Also added with JEP 330 is the ability to configure a single-file source-code application as a shebang file. This requires removing the .java file extensions, and at the top of the file, adding a shebang #!, the path to the Java install, and the source version:

#! /path/to/java/install  --source version

Interactive Input

Like any Java application, values can be passed into a utility application at startup. This could be enough for many applications, but if an interactive command-line experience is desired or needed, the java.io.Console class provides some helpful APIS for this:

User Input

String readLine() - Reads user input String readLine(String, Object...) - Prints formatted message, reads user input String readPassword() - Reads user input, echo disabled String readPassword(String, Object...) - Prints formatted message, reads user input, echo disabled

Output

printf(String, Object ...) - Prints a formatted message format(String, Object ...) - Prints a formatted message

File I/O

Often scripts will need to interact with the file system; search and update files, generate file structures, delete files, and others; for this, the java.io package has many options for reading and writing. The complete list of available classes can be found in the JavaDoc for the java.io package.

Improved I/O APIs

The java.io package has been part of the JDK since 1.0. Perhaps unsurprisingly, some of the classes and APIs in this package might be showing their age and don’t always provide a great developer experience. It might be worth considering some of the classes in the java.nio.file package when traversing the file system or performing some tasks like creating directories:

  • Path - An object that may be used to locate a file in a file system.

  • Paths - This class consists exclusively of static methods that return a Path by converting a path string or URI.

  • Files - This class consists exclusively of static methods that operate on files, directories, or other types of files.

Additional Readings

Happy coding!