Using Java 9 HTTP Client with JUnit and Maven

Java 9 comes with built-in HTTP (2) client (incubator status):

import java.nio.charset.Charset;
import java.time.Duration;
import jdk.incubator.http.HttpClient;
import jdk.incubator.http.HttpRequest;
import jdk.incubator.http.HttpResponse;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;

public class HttpClientTest {

    private HttpClient client;

    public void init() {
        this.client = HttpClient.newHttpClient();

    public void get() throws IOException, InterruptedException {
        URI uri = URI.create("");
        HttpRequest getRequest = HttpRequest.newBuilder(uri).
        HttpResponse<String> response = this.client.send(getRequest,
        String payload = response.body();
        assertThat(payload, containsString("java"));

The HTTP client resides in jdk.incubator.httpclient module and has to be "required". The resides in src/main/java:

module com.airhacks.http {
    requires jdk.incubator.httpclient;

You will need the maven-compiler-plugin at least in the version > 3.6.1


Now the unit tests compiles, but the execution fails with:

java.lang.NoClassDefFoundError: Ljdk/incubator/http/HttpClient;
    at java.base/java.lang.Class.getDeclaredFields0(Native Method)
    at java.base/java.lang.Class.privateGetDeclaredFields(
    at java.base/java.lang.Class.getDeclaredFields(    

Although the module is declared, it has to be still added with the following JVM argument:

    <argLine>--add-modules jdk.incubator.httpclient</argLine>
See you at Java EE 8 on Java 9, at Munich Airport, Terminal 2

NEW online workshop: Web Components training (online)

Airport MUC workshops: Java EE 8, Java 9: Bootstrap, Effective, Architectures, Single Page Apps, Progressive Web Apps, HTML 5, ES 6, CSS 3 and Microservices

Podcast: and newsletter:

A book about rethinking Java EE Patterns


Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments