{"id":10534,"date":"2020-09-24T00:11:31","date_gmt":"2020-09-23T18:41:31","guid":{"rendered":"https:\/\/java2blog.com\/?p=10534"},"modified":"2021-01-11T18:29:04","modified_gmt":"2021-01-11T12:59:04","slug":"java-9-process-api-improvements","status":"publish","type":"post","link":"https:\/\/java2blog.com\/java-9-process-api-improvements\/","title":{"rendered":"Java 9 &#8211; Process API Improvements"},"content":{"rendered":"<p>In this post, we will see about Java 9 process API improvements.<br \/>\n<div id=\"toc_container\" class=\"toc_light_blue no_bullets\"><p class=\"toc_title\">Table of Contents<\/p><ul class=\"toc_list\"><li><a href=\"#The_Process_Class\">The Process Class<\/a><ul><li><a href=\"#Start_a_new_Process\">Start a new Process?<\/a><\/li><li><a href=\"#Methods\">Methods<\/a><\/li><li><a href=\"#Example_to_get_Process_Id\">Example to get Process Id<\/a><\/li><li><a href=\"#Example_to_get_Process_Information\">Example to get Process Information<\/a><\/li><\/ul><\/li><li><a href=\"#ProcessHandle_Interface\">ProcessHandle Interface<\/a><ul><li><a href=\"#Methods-2\">Methods<\/a><\/li><li><a href=\"#Example\">Example<\/a><\/li><\/ul><\/li><li><a href=\"#ProcessHandleInfo_Interface_Methods\">ProcessHandle.Info Interface Methods<\/a><\/li><\/ul><\/div>\n\nJava improved its Process API in Java 9 version that includes new methods for <code>Process class<\/code> and two new interfaces <code>ProcessHandle<\/code> and <code>ProcessHandle.Info<\/code>. These methods are used to create a new process and get process information like process status, running time, process id, etc. We can also get the current running process and its information.<\/p>\n<h2><span id=\"The_Process_Class\">The Process Class<\/span><\/h2>\n<p>Java <code>Process<\/code> class is located in <code>java.lang package<\/code> and provides methods to control the processes started by <code>ProcessBuilder.start<\/code> and <code>Runtime.exec<\/code>.<\/p>\n<h3><span id=\"Start_a_new_Process\">Start a new Process?<\/span><\/h3>\n<p>We can call <code>start()<\/code> method of <code>ProcessBuilder<\/code> class to start a new process. It will return an instance of <code>Process<\/code> class that further can be used to get process-related information.<\/p>\n<pre code=\"java\">\nProcess process = new ProcessBuilder(\"vim\").start(); \/\/ vim is an editor in Linux\n<\/pre>\n<h3><span id=\"Methods\">Methods<\/span><\/h3>\n<p>Although <code>Process<\/code> class contains several methods but here we are listing the new methods added into Java 9 version. <\/p>\n\n<table id=\"tablepress-33\" class=\"tablepress tablepress-id-33\">\n<thead>\n<tr class=\"row-1 odd\">\n\t<th class=\"column-1\">Method<\/th><th class=\"column-2\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-hover\">\n<tr class=\"row-2 even\">\n\t<td class=\"column-1\">boolean supportsNormalTermination()<\/td><td class=\"column-2\">It returns true if the implementation of destroy() is to normally terminate the process, else returns false.<\/td>\n<\/tr>\n<tr class=\"row-3 odd\">\n\t<td class=\"column-1\">ProcessHandle toHandle()<\/td><td class=\"column-2\">It returns a ProcessHandle for the Process.<\/td>\n<\/tr>\n<tr class=\"row-4 even\">\n\t<td class=\"column-1\">long pid()<\/td><td class=\"column-2\">It returns the native process ID of the process.<\/td>\n<\/tr>\n<tr class=\"row-5 odd\">\n\t<td class=\"column-1\">Stream children()<\/td><td class=\"column-2\">It returns a snapshot of the direct children of the process.<\/td>\n<\/tr>\n<tr class=\"row-6 even\">\n\t<td class=\"column-1\">Stream descendants()<\/td><td class=\"column-2\">It returns a snapshot of the descendants of the process.<\/td>\n<\/tr>\n<tr class=\"row-7 odd\">\n\t<td class=\"column-1\">ProcessHandle.Info info()<\/td><td class=\"column-2\">It returns a snapshot of information about the process.<\/td>\n<\/tr>\n<tr class=\"row-8 even\">\n\t<td class=\"column-1\">CompletableFuture onExit()<\/td><td class=\"column-2\">It returns a CompletableFuture for the termination of the Process.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-33 from cache -->\n<h3><span id=\"Example_to_get_Process_Id\">Example to get Process Id<\/span><\/h3>\n<p>After creating a process (in the above example), we are getting process id using <code>pid()<\/code> method of <code>Process<\/code> class. See the example below.<\/p>\n<pre code=\"java\">\nimport java.io.IOException;\n\npublic class Main{\n    public static void main(String[] args) throws IOException {\n        Process process = new ProcessBuilder(\"vim\").start();\n         \/\/ Get process id\n         System.out.println(process.pid());\n    }\n}\n<\/pre>\n<p>Output<\/p>\n<div class=\"content-box-green\">\nProcess Id: 10754\n<\/div>\n<h3><span id=\"Example_to_get_Process_Information\">Example to get Process Information<\/span><\/h3>\n<p>Here is an example to get more information about the process such as hashcode, children and class.<\/p>\n<pre code=\"java\">\nimport java.io.IOException;\n\npublic class Main{\n    public static void main(String[] args) throws IOException {\n        Process process = new ProcessBuilder(\"vim\").start();\n         \/\/ Get created process info\n         System.out.println(process.pid());\n         System.out.println(process.info());\n         System.out.println(process.hashCode());\n         System.out.println(process.isAlive());\n         System.out.println(process.children());\n         System.out.println(process.getClass());\n         System.out.println(process.descendants().count());\n    }\n}\n<\/pre>\n<p>Output<\/p>\n<div class=\"content-box-green\">\n15066<br \/>\n[user: Optional[irfan], cmd: \/usr\/bin\/vim.basic, startTime: Optional[2020-09-17T07:50:15.080Z], totalTime: Optional[PT0S]]\n1418481495<br \/>\ntrue<br \/>\njava.util.stream.ReferencePipeline$2@65ab7765<br \/>\nclass java.lang.ProcessImpl<br \/>\n0\n<\/div>\n<h2><span id=\"ProcessHandle_Interface\">ProcessHandle Interface<\/span><\/h2>\n<p>This <a href=\"https:\/\/java2blog.com\/interface-in-java-with-example\/\" target=\"_blank\" rel=\"noopener noreferrer\">interface<\/a> is added into Java 9 to provide control of native or created processes. It provides additional support for process handling. We can use its <code>current()<\/code> method to get current process handler. Let&#8217;s see an example.<\/p>\n<pre code=\"java\">\npublic class Main{\n    public static void main(String[] args) {\n        ProcessHandle processHandle = ProcessHandle.current(); \/\/ current process\n        System.out.println(\"Process Id: \"+processHandle.pid());\n    }\n}\n<\/pre>\n<p>Output<\/p>\n<div class=\"content-box-green\">\nProcess Id: 17044\n<\/div>\n<h3><span id=\"Methods-2\">Methods<\/span><\/h3>\n<p>The following are the methods of <code>ProcessHandle<\/code> Interface.<\/p>\n\n<table id=\"tablepress-34\" class=\"tablepress tablepress-id-34\">\n<thead>\n<tr class=\"row-1 odd\">\n\t<th class=\"column-1\">Method<\/th><th class=\"column-2\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-hover\">\n<tr class=\"row-2 even\">\n\t<td class=\"column-1\">static Stream allProcesses()<\/td><td class=\"column-2\">It returns a snapshot of all processes visible to the current process.<\/td>\n<\/tr>\n<tr class=\"row-3 odd\">\n\t<td class=\"column-1\">Stream children()<\/td><td class=\"column-2\">It returns a snapshot of the current direct children of the process.<\/td>\n<\/tr>\n<tr class=\"row-4 even\">\n\t<td class=\"column-1\">int compareTo(ProcessHandle other)<\/td><td class=\"column-2\">It compares this ProcessHandle with the specified ProcessHandle for order.<\/td>\n<\/tr>\n<tr class=\"row-5 odd\">\n\t<td class=\"column-1\">static ProcessHandle current()<\/td><td class=\"column-2\">It returns a ProcessHandle for the current process.<\/td>\n<\/tr>\n<tr class=\"row-6 even\">\n\t<td class=\"column-1\">Stream descendants()<\/td><td class=\"column-2\">It returns a snapshot of the descendants of the process.<\/td>\n<\/tr>\n<tr class=\"row-7 odd\">\n\t<td class=\"column-1\">boolean destroy()<\/td><td class=\"column-2\">It requests the process to be killed.<\/td>\n<\/tr>\n<tr class=\"row-8 even\">\n\t<td class=\"column-1\">boolean destroyForcibly()<\/td><td class=\"column-2\">It requests the process to be killed forcibly.<\/td>\n<\/tr>\n<tr class=\"row-9 odd\">\n\t<td class=\"column-1\">boolean equals(Object other)<\/td><td class=\"column-2\">It returns true if other object is non-null, is of the same implementation, and represents the same system process; otherwise it It returns false.<\/td>\n<\/tr>\n<tr class=\"row-10 even\">\n\t<td class=\"column-1\">int hashCode()<\/td><td class=\"column-2\">It returns a hash code value for this ProcessHandle.<\/td>\n<\/tr>\n<tr class=\"row-11 odd\">\n\t<td class=\"column-1\">ProcessHandle.Info info()<\/td><td class=\"column-2\">It returns a snapshot of information about the process.<\/td>\n<\/tr>\n<tr class=\"row-12 even\">\n\t<td class=\"column-1\">boolean isAlive()<\/td><td class=\"column-2\">It tests whether the process represented by this ProcessHandle is alive.<\/td>\n<\/tr>\n<tr class=\"row-13 odd\">\n\t<td class=\"column-1\">static Optional of(long pid)<\/td><td class=\"column-2\">It returns an Optional for an existing native process.<\/td>\n<\/tr>\n<tr class=\"row-14 even\">\n\t<td class=\"column-1\">CompletableFuture onExit()<\/td><td class=\"column-2\">It returns a CompletableFuture for the termination of the process.<\/td>\n<\/tr>\n<tr class=\"row-15 odd\">\n\t<td class=\"column-1\">Optional parent()<\/td><td class=\"column-2\">It returns an Optional for the parent process.<\/td>\n<\/tr>\n<tr class=\"row-16 even\">\n\t<td class=\"column-1\">long pid()<\/td><td class=\"column-2\">It returns the native process ID of the process.<\/td>\n<\/tr>\n<tr class=\"row-17 odd\">\n\t<td class=\"column-1\">boolean supportsNormalTermination()<\/td><td class=\"column-2\">It returns true if the implementation of destroy() normally terminates the process.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-34 from cache -->\n<h3><span id=\"Example\">Example<\/span><\/h3>\n<p>We will use <code>ProcessHandle<\/code> interface to get more information of currently running process.<\/p>\n<pre code=\"java\">\npublic class Main{\n    public static void main(String[] args) {\n        ProcessHandle processHandle = ProcessHandle.current();\n        System.out.println(\"Get Process Info: \");\n        System.out.println(\"Process Id: \"+processHandle.pid());\n        ProcessHandle.Info pHandleInfo = processHandle.info();\n        System.out.println(pHandleInfo.arguments().isPresent());\n        System.out.println(pHandleInfo.arguments().isEmpty());\n        System.out.println(pHandleInfo.command().isPresent());\n        System.out.println(pHandleInfo.totalCpuDuration().get());\n        \/\/ process user name\n        System.out.println(pHandleInfo.user().get());\n    }\n}\n<\/pre>\n<p>Output<\/p>\n<div class=\"content-box-green\">\nProcess Id: 12437<br \/>\ntrue<br \/>\nfalse<br \/>\ntrue<br \/>\nPT0.11S<br \/>\nirfan\n<\/div>\n<h2><span id=\"ProcessHandleInfo_Interface_Methods\">ProcessHandle.Info Interface Methods<\/span><\/h2>\n<p>It is a nested interface of <code>ProcessHandle<\/code> interface and used to provide support for <code>Process<\/code> handling. <\/p>\n\n<table id=\"tablepress-35\" class=\"tablepress tablepress-id-35\">\n<thead>\n<tr class=\"row-1 odd\">\n\t<th class=\"column-1\">Method<\/th><th class=\"column-2\">Description<\/th>\n<\/tr>\n<\/thead>\n<tbody class=\"row-hover\">\n<tr class=\"row-2 even\">\n\t<td class=\"column-1\">Optional arguments()<\/td><td class=\"column-2\">It returns an array of Strings of the arguments of the process.<\/td>\n<\/tr>\n<tr class=\"row-3 odd\">\n\t<td class=\"column-1\">Optional command()<\/td><td class=\"column-2\">It returns the executable pathname of the process.<\/td>\n<\/tr>\n<tr class=\"row-4 even\">\n\t<td class=\"column-1\">Optional commandLine()<\/td><td class=\"column-2\">It returns the command line of the process.<\/td>\n<\/tr>\n<tr class=\"row-5 odd\">\n\t<td class=\"column-1\">Optional startInstant()<\/td><td class=\"column-2\">It returns the start time of the process.<\/td>\n<\/tr>\n<tr class=\"row-6 even\">\n\t<td class=\"column-1\">Optional totalCpuDuration()<\/td><td class=\"column-2\">It returns the total cputime accumulated of the process.<\/td>\n<\/tr>\n<tr class=\"row-7 odd\">\n\t<td class=\"column-1\">Optional user()<\/td><td class=\"column-2\">It returns the user of the process.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<!-- #tablepress-35 from cache -->\n<p>That&#8217;s all about Java 9 Process API improvements.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Table of ContentsThe Process ClassStart a new Process?MethodsExample to get Process IdExample to get Process InformationProcessHandle InterfaceMethodsExampleProcessHandle.Info Interface Methods In this post, we will see about Java 9 process API improvements. Java improved its Process API in Java 9 version that includes new methods for Process class and two new interfaces ProcessHandle and ProcessHandle.Info. These [&hellip;]<\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"_mi_skip_tracking":false},"categories":[201],"tags":[],"_links":{"self":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/10534"}],"collection":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/comments?post=10534"}],"version-history":[{"count":0,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/posts\/10534\/revisions"}],"wp:attachment":[{"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/media?parent=10534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/categories?post=10534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/java2blog.com\/wp-json\/wp\/v2\/tags?post=10534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}