Java 9: Hello World!

Java 9 is scheduled for release in 2017, and is shaping up to have some exciting new features. If you’re excited to try it out too, here are the first steps to compiling some real live Java 9 code!

Download Java 9

First, download Java 9 from OpenJDK. At one point, Project Jigsaw was in a separate branch from the main Java 9 download, but it was merged into the main trunk in March 2016. So rest assured, you are getting the whole ball of wax with this download! Once downloaded, we can set the path to JDK/bin like we can for any other Java installation.

Uh, What Now?

Now, you might be thinking about how to edit or build a Java 9 program since it’s not supported by major IDE’s or build tools yet. Not to worry: for purposes of just seeing what it can do, we can use a good ‘ol text editor and build with the javac command from the command line like we did back in the 90’s 😉 Once the code is written (see below, if you’re eager), we will build and run it like so:

javac; java Main

Hello World

It would be nice to build a “Hello World” with Java 9, but what can we build that we can only do in Java 9? One feature that caught my eye was JEP-102: Process API Updates, which has been sorely missed for years. So using this functionality seems like a nice first step and will let us try something Java-9-only, while still being immediately useful to us when Java 9 is released.

Finally: Java 9 is Here

Here is a complete class, feel free to copy and paste at will, and we will explore it a bit below:

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

public class Main {

   public static void main(String[] args) throws Exception {

	System.out.println("Hello Java 9! This process PID is " + ProcessHandle.current().getPid());

	ProcessBuilder builder = new ProcessBuilder("ls");

	Process process = builder.start();

	try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
	 	String line;
		while ((line = in.readLine()) != null) {

	// define a function to call when the child process ends
	// and block until that function completes
		.whenComplete( Main::mainFinished )


   public static void mainFinished(Process p, Throwable t) {
      System.out.println("process " + p.getPid() + " finished.");


What Just Happened?

ProcessHandle is a new class in Java 9, think of it as an extension of the Process class. For reference, you can read up on the Javadocs. ProcessHandle.current() returns the ProcessHandle object representing the current native process running this thread, and getPid() (as you might have guessed) returned the process ID of the native process. So if you want to know the PID of the currently running Java process, just call ProcessHandle.current().getPid(). Easy!

Next, we build a simple process, run it, and retrieve the output of it. This happens like any other Java program… nothing to see here, move along.

Finally, something interesting happens: The Java 9 Process class now provides an onExit() hook so we can execute a lambda when the native process has exited. In this case, we’ll just do something simple like write out the PID of the child process which just exited. Note that the main thread could finish before this lambda gets a chance to run, so we called CompletableFuture.get() to block the current thread until the onExit() lambda finishes.


Java 9 has a lot of new functionality that could prove to be very useful. This post is just to whet your appetite and show you how easy it is to get started with Java 9 TODAY! In future posts we will be exploring more of the functionality in Java 9… stay tuned!

Leave a comment

Filed under Java 9

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s