Java 9 Process API

Java 9 will come with interesting features. Here is a quick review of the new Java 9 Process API.

Use Cases for the Java Process API

These are probably some ways how we are using the Java Process API:

  • To Install, configure or to communicate to other programs from Java: For example, I implemented a Java Installer to install the WebSphere Application Server into the system, and then I configure it by running a script coded in Jython executed the wsadmin tool.
  • To install specific Operating System stuff. I didn’t find any other simpler way to install Windows start menu items from Java other than by doing it in a VBScript program.
  • In my opinion, the Java Process API is in general used to integrate Java with other programs or processes. For example. I made a game in Blender and I implemented a launcher in Java. So, from Java I am calling the blender.exe passing the game.blender file as argument.

Known Problems and Workarounds

  • There are common issues in Windows when invoking commands which have spaces or stream redirections. The solution is to use instead the “cmd /C” command. This oracle blog talks about this. There is also an Apache commons-exec library apparently solving the same issues, but its kind of abandoned project.
  • Another issue when running processes is how to read from standard output and error streams properly. The solution is basically creating separate threads for reading the streams so the process don’t get stucked. There is an elegant solution in this blog.
  • On the other “side” (literally), if we want to kill a Java Process from the Operating System we may use the Java jps command to get the PID. Then, for instance, we can use kill -9 to kill it.
  • Finally. Sometimes when calling a Process it ends up hanging. Hopefully the Java 9 implementation will be more stable about this issue.

Java 9 Specification

Taking a look at the new spec I have to say the 4 new features look really nice! Having that features available, we will be able to implement a pure Java Windows “Task Manager” like; because now we will be able to get rid of some process calls to get process data.

Getting started with Java 9

  • Download the Java 9 version from
  • Install java 9 on folder /usr/lib/jvm/java-9-oracle
  • Check installation by running /usr/lib/jvm/java-9-oracle/bin/java -version

Code examples

So after reading about the new spec I got hands on. I compared the Javadoc for the 8 version and the new Java 9 and I found interesting classes like java.lang.ProcessHandle & java.lang.Process.Info. Lets see what we can do with them! (the full source code for the examples is located here):

Get PID of current and created processes

System.out.println("Running java process with PID " + ProcessHandle.current().getPid()+ ". Parent: " + ProcessHandle.current().parent().get().getPid());
Process pr = Runtime.getRuntime().exec(cmdArray);
System.out.println("Create Process with PID "+pr.getPid());

Enumerate Process List

ProcessHandle.allProcesses().forEach(e -> System.out.println(e.getPid() + " " +;

Deal with Process Trees

In this case, we want to deploy a process tree in order to kill everything afterwards. For this, we can implement a Java Process that creates the same Java Process recursively, finally we kill it.
Process ptree = RunHelper.exec(java, output, "-cp", cp, RecursiveJavaProcess.class.getName(), "3");

Compile & execute!

$ cd src/test/java
$ /usr/lib/jvm/java-9-oracle/bin/javac org/andresoviedo/tests/java9/process/
$ /usr/lib/jvm/java-9-oracle/bin/javac org/andresoviedo/tests/java9/process/
$ /usr/lib/jvm/java-9-oracle/bin/java org.andresoviedo.tests.java9.process.NewJava9ProcessAPI


From the main 4 features the spec mentions, I have tested the first 3. So where is the 4th? About dealing with hundred of sub-processes? I didn’t find in the current version any new option for doing that. Probably they are still implementing it. I don't know. However, if we want, we can now implement very easily a pure Java Task Manager GUI application for managing the system processes :)

Although I would like to know more real complex use cases to further evaluate the API, so far the promised features seems to work. Anyway, I am wondering what else would be nice to have for the future in the API. Probably a way to manage the process priority? I don’t know. For me, the API looks quite complete for now. I hope you like the new features.

Feel free to leave your comments. Thank you for reading me :)


  1. We've been waiting so long for Java 9 and it's finally here! And it's as awesome as I always imagined it would be :)

  2. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training Core Java 8 Training in Chennai Core java 9 online training JavaEE Training in Chennai Java EE Training in Chennai

  3. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it. Java Training in Chennai

  4. It is no understatement to say that online gaming, and the Internet as a whole, took a massive step forward as a consequence of those two computing languages.

  5. Nice blog, here I had an opportunity to learn something new in my field. I have an expectation about your future post so please keep updates...Thanks..
    Web Designing Training in Chennai | Python Training in Chennai

  6. Your posts is really helpful for me.Thanks for your wonderful post. I am very happy to read your post. It is really very helpful for us and I have gathered some important information from this blog. Hadoop Training in Chennai | Salesforce Training in Chennai

  7. Your post is very nice. I read your blog completely it is one of the fantstic blog. Thanks for sharing.keep sharing more blogs.

    Python Online Training

  8. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..

    Informatica Training In Chennai | Hadoop Training In Chennai | Sap MM Training In Chennai


  9. Hi,
    Thanks for sharing such an informative blog. I have read your blog and I gathered some needful information from your blog.

    sap interview questions

  10. conventional medicine
    A decent blog dependably thinks of new and energizing data and keeping in mind that understanding I have feel that this blog is truly have each one of those quality that qualify a blog to be an one.I needed to leave a little remark to help you and wish you a decent continuation. Wishing you the good luck for all your blogging endeavors.

  11. Accountants Hove
    After I read and endeavor to comprehend this article lastly amazingwe are for the most part appreciative for the presence of this article can include significantly more knowledge for every one of us. much obliged to you

  12. It is really a great work and the way in which u r sharing the knowledge is excellent. Thanks for helping me to understand basic concepts. Thanks for your informative article. Java Training in Chennai | Pega Training in Chennai

  13. Personally, I really liked this casino exclusively best online casino especially the choice of games. All games are licensed and with normal returns. I really like how you can choose one-click games here and it’s like a lot of slot machines and slots in your hand.

  14. it is an interesting Article, i read this post that was very nice and useful thanks for sharing it.
    e learning

  15. thanks for your information really good and very nice web design company in velachery


Post a Comment

Popular posts from this blog

How to build a replicated ehcache through firewall and not to die trying

Android 3D Model Viewer Open Source

Google Drive FTP Adapter