Core Java

Experimenting with Java9 HTTP Client and Process API in JShell

This post continues my exploration of Java9 features from my My Top Java 9 Features blog post. Here we are experimenting with Java9 HTTP/2 Client and Process API in JShell

HTTP/2 Client

The HTTP/2 Client is an incubator project in Java9. This means the API isn’t finalized, so has some scope for change in future versions. The most obvious changes from Java9 to Java10 will be moving it from the jdk.incubator.httpclient module to “http.client” module, plus associated package name changes. Its worth keeping this in mind when using the API.

The HTTP/2 doesnt work straight out the box in jshell, but its ok as it lets us see the Java Platform Modularity System(JPMS) in action. We simply pass the httpclient module into jshell using –add-modules –

C:\jdk9TestGround>jshell -v --add-modules jdk.incubator.httpclient

We then import the http libraries –

jshell> import jdk.incubator.http.*;

We can now call a website from jshell –

jshell> HttpClient httpClient = HttpClient.newHttpClient();
jshell> HttpRequest httpRequest = HttpRequest.newBuilder().uri(new URI("")).GET().build();
jshell> HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandler.asString());
jshell> System.out.println(httpResponse.statusCode());
jshell> System.out.println(httpResponse.body());

The most interesting feature is the use of the Builder pattern to create the HTTP Request. This is defined in HttpRequest.Builder which can be used to construct more complex HttpClient requests including authentication.

The syntax is similar to both the Jetty HttpClient and okhttp which are Http/2 compliant. It is definitely much simpler than the old approach in Java.

Other useful features of this API are –

  • Asynchronous Requests – This is more useful than the above example as it is non-blocking. This is done through the HttpRequest.sendAsync method
  • WebSockets – This is created through the WebSocket class which has its own WebSocket.Builder. I’m going to cover this in another post as its clearer than on jshell

Process API

The process API simplifies accessing Process information in Java.

Consider the details of my current Jshell process –

jshell> System.out.println(ProcessHandle.current().pid());
jshell> System.out.println(ProcessHandle.current().info());
[user: Optional[NEW-EJ0JTJ5I9B9\javabullets], cmd: C:\Program Files\Java\jdk-9\bin\java.exe, startTime: Optional[2017-10-09T19:41:21.743Z], totalTime: Optional[PT4.625S]]
jshell> System.out.println(ProcessHandle.current().parent());

We can also access System processes and Id’s –

jshell> ProcessHandle.allProcesses().forEach(p -> System.out.println(;

Or Info –

jshell> ProcessHandle.allProcesses().forEach(p -> System.out.println(;
[user: Optional[NEW-EJ0JTJ5I9B9\javabullets], cmd: C:\Program Files (x86)\PFU\ScanSnap\Update\ScanSnapUpdater.exe, startTime: Optional[2017-10-09T18:28:42.812Z], totalTime: Optional[PT0.78125S]]
[user: Optional[NEW-EJ0JTJ5I9B9\javabullets], cmd: C:\Windows\explorer.exe, startTime: Optional[2017-10-09T18:35:08.397Z], totalTime: Optional[PT25.234375S]]
[user: Optional[NEW-EJ0JTJ5I9B9\javabullets], cmd: C:\Windows\System32\cmd.exe, startTime: Optional[2017-10-09T18:36:11.522Z], totalTime: Optional[PT0.078125S]]

Now we have access to processes we can kill selective process – lets kill notepad –

jshell> Process p = new ProcessBuilder("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe").start();
p ==> Process[pid=9644, exitValue="not exited"]
jshell> p.destroy();

We also have the option to destroyForcibly if destroy doesnt kill the process.

The above examples show how useful and simple the Process API is for starting, killing and monitoring processes. It free’s us from relying on third party libraries for providing process control

Published on Java Code Geeks with permission by Martin Farrell, partner at our JCG program. See the original article here: Experimenting with Java9 HTTP Client and Process API in JShell

Opinions expressed by Java Code Geeks contributors are their own.

Martin Farrell

Martin is a Software Developer and Consultant specializing in Java and the Spring Framework. When not coding he spends time with his wife and two children, or cycling his bike.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Back to top button