Search code examples
javajava-9java-http-client

Java 9 no class definition exception


So i want to try the http client

package com.company;

import jdk.incubator.http.HttpClient;

public class Main {

public static void main(String[] args) {
    HttpClient client =  HttpClient.newHttpClient();

  }
}

And my module info looks like this

module com.company {
    requires jdk.incubator.httpclient;
}

But i get java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient

And I don't really understand why. My java version is "build 9-ea+ 169" and I use the latest version of IntelliJ idea (2017.1.3). I looked into this answer and it looks like I have to just add requirement into a file, but it doesn't work for some reason.


Solution

  • works fine for me if I use --add-modules jdk.incubator.httpclient as the start-up parameter.

    HttpClient client = HttpClient.newHttpClient();
    client.executor().execute(() -> System.out.println("Here")); // prints Here
    

    If you say that your module requires it, does not mean it will be included; at it is not included by default.