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 https://jdk9.java.net/download/.
  • 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() + " " + e.info().commandLine()));

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");
Thread.sleep(4500);
ptree.destroy();

Compile & execute!

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

Conclusion


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 :)

Comments

  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 :)

    ReplyDelete
    Replies
    1. IEEE Final Year projects Project Center in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. <Final Year Projects for CSE It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, project projects for cseAngular Directives, Angular Services, Pipes, security fundamentals, Routing, and Angular programmability. The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  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

    ReplyDelete
  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

    ReplyDelete
  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.
    StartPoint

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete

  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

    ReplyDelete
  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.

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete
  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.

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

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

    ReplyDelete
  16. This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted.By Learn Digital Marketing Course Training in Chennai it will help to get Digital Marketing Course Training in Chennai.Learn Best Digital Marketing Course Training in Chennai's professional institute to get reputed job.

    ReplyDelete
  17. Thank you for excellent article.You made an article that is interesting.
    Best AWS certification training courses. Build your AWS cloud skills with expert instructor- led classes. Live projects, Hands-on training,24/7 support.
    https://onlineidealab.com/aws-certification/

    ReplyDelete

  18. Awesome,Thank you so much for sharing such an awesome blog.web designing training in bangalore

    ReplyDelete
  19. Such a great information for blogger i am a professional blogger thanks…

    Looking for Best Training Institute in Bangalore , India. Softgen Infotech is the best one to offers 85+ computer training courses including IT Software Course in Bangalore , India. Also it provides placement assistance service in Bangalore for IT.

    ReplyDelete
  20. Great Article. Thank you for sharing! Really an awesome post for every one.

    IEEE Final Year projects Project Centers in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes, while specialists like the enjoyment in interfering with innovation. For experts, it's an alternate ball game through and through. Smaller than expected IEEE Final Year project centers ground for all fragments of CSE & IT engineers hoping to assemble. Final Year Project Domains for IT It gives you tips and rules that is progressively critical to consider while choosing any final year project point.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai

    ReplyDelete
  21. keep up the good work. this is an Assam post. this to helpful, i have reading here all post. i am impressed. thank you. this is our digital marketing training center. This is an online certificate course
    digital marketing training in bangalore | https://www.excelr.com/digital-marketing-training-in-bangalore

    ReplyDelete
  22. Really useful information.

    Data science Course in Mumbaii

    Thank You Very Much For Sharing These Nice Tips.

    ReplyDelete
  23. Such a wonderful article and I feel that it is best to write more on this topic. Thank you so much because i learn a lot of ideas about it. Keep posting...
    Digital Marketing Course In Kolkata
    Web Design Course In Kolkata
    SEO Course In Kolkata

    ReplyDelete

  24. That is nice article from you , this is informative stuff . Hope more articles from you . I also want to share some information about cognos training online and websphere portal tutorial

    ReplyDelete
  25. This was really one of my favorite website. Please keep on posting. ExcelR Digital Marketing Course Pune

    ReplyDelete
  26. Effective blog with a lot of information. I just Shared you the link below for Courses .They really provide good level of training and Placement,I just Had Digital Marketing Classes in this institute,Just Check This Link You can get it more information about the Digital Marketing course.


    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  27. Excellent Blog!!! The blog which you have shared here is more informative, This is really too useful and have more ideas and keep sharing many techniques about java. Thanks for giving a such a wonderful blog.
    Java training in chennai | Java training in annanagar | Java training in omr | Java training in porur | Java training in tambaram | Java training in velachery

    ReplyDelete
  28. There are many things mentioned in this article I would not have thought of on my own. This material is inspirational, interesting and it allows the readers to open up their minds to original thinking.
    Best Data Science training in Mumbai

    Data Science training in Mumbai

    ReplyDelete
  29. Good post. I learn something new and challenging on sites I stumbleupon on a daily basis. It's always interesting to read content from other writers and practice a little something from their web sites.
    Digital Marketing Training Course in Chennai | Digital Marketing Training Course in Anna Nagar | Digital Marketing Training Course in OMR | Digital Marketing Training Course in Porur | Digital Marketing Training Course in Tambaram | Digital Marketing Training Course in Velachery

    ReplyDelete
  30. Awesome,Thank you so much for sharing such an awesome blog.Bedroom Furniture

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

    Kids Toys

    ReplyDelete
  32. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.
    Data Science Institute in Bangalore

    ReplyDelete
  33. Really nice and interesting post. I was looking for this kind of information and enjoyed reading this one. Keep posting. Thanks for sharing.
    Data Science Certification in Bangalore

    ReplyDelete
  34. Regular visits listed here are the easiest method to appreciate your energy, which is why why I am going to the website everyday, searching for new, interesting info. Many, thank you!

    Data Science Course

    ReplyDelete
  35. Impressive. Your story always bring hope and new energy. Keep up the good work.

    Data Science Training

    ReplyDelete
  36. Such a very useful article. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    Data Science Training Institute in Bangalore

    ReplyDelete
  37. This is a wonderful article, Given so much info in it, These type of articles keeps the users interest in the website, and keep on sharing more ... good luck.

    AWS training in Chennai

    AWS Online Training in Chennai

    AWS training in Bangalore

    AWS training in Hyderabad

    AWS training in Coimbatore

    AWS training


    ReplyDelete

Post a Comment

Popular posts from this blog

Android 3D Model Viewer Open Source

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