Java9 : Process API Updates #1

Reading Time: 3 minutes

Process API is a feature of Java which already exists until Java 8, but with the release of Java 9, there were a lot of new features which came up.

Issue with Process API until Java 8

What if I want to develop a device driver software or Operating System? Interaction with system level component was not suitable until Java 8. The process communication with Java was extremely difficult since it was not a machine friendly language. In order to communicate with the process, we need to write kilometres of code and most of which was native code, not written in Java and you need to use a lot of jar files. Therefore, until Java 8, the communication with the underlying operating system processes was not an easy task.

Also, the way process management and handling was done varied from operating system to operating system.

Resolution with Java 9

Now, with the updates the Java 9 has provided, it is as simple as writing a simple hello world program, our first code when we started learning programming. So, let’s begin as to how all these issues got resolved in Java 9.

In this blog, we will be discussing about 2 features which exists until Java 8 but had new features added in Java 9.

  1. Process Class : Under this, several new methods were introduced such as pid(), info() etc.
  2. ProcessBuilder : This feature is used to create our own java process. You can control one java or non-java process using another java process.

Let’s see an example on how we can create and destroy a Java code using another Java code using the above methods in order to have a better understanding.

Our first Java code is more of a GUI code which just creates a frame and adds the label : This Process is started from ProcessBuilder.

import java.awt.*;
import java.awt.event.*;
public class FrameDemo {
	public static void main(String[] args) {
		Frame frame = new Frame();
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		frame.add(new Label("This Process is started from ProcessBuilder."));
		frame.setSize(500,500);
		frame.setVisible(true);
	}
}

Now we will be creating a new code, which will be starting this particular code and will destroy it after 10 seconds. In the code below, we are creating a new process using ProcessBuilder. In order to create one, we need to pass the command which is used to create the process. Since, in our particular example, we want it to start the FrameDemo.java class, the command will be java along with the path of where the .java file is present. We have also extracted the pid of the process which will be created.

public class TestProcessCreateDestroy {
	public static void main(String[] args) throws Exception {
		ProcessBuilder processBuilder = new ProcessBuilder("java", "/home/knoldus/Documents/Java9/knolx/FrameDemo.java");
		Process process = processBuilder.start();
		System.out.println("Process started having pid : " + process.pid());
		Thread.sleep(10000);
		System.out.println("Destroying process having pid : " + process.pid());
		process.destroy();
	}
}

Thus, when you will run the TestProcessCreateDestroy.java file, a frame will be created (as shown below) and after 10 seconds, it will be destroyed on its own.

Hence, we are able to create and destroy a Java program using another Java program. You can pick up any code of your own choice and play around it. Happy Blogging!

References:

Written by 

Jasmine is a Software Consultant, with an experience of more than 11 months. She is familiar with Object Oriented Programming Paradigms and has a good command over C/C++. Apart from being a programmer, She is also fond of relational database technologies such as PostGreSQL & MySQL. Apart from that, work life balance is one of the most important thing for her. Her hobbies include watching netflix,chatting ,surfing net and writing.