Java 9 no class definition exception
Asked Answered
A

3

12

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.

Asiaasian answered 18/5, 2017 at 7:50 Comment(2)
@QBrute the module jdk.incubator.httpclient contains the package jdk.incubator.http, see download.java.net/java/jdk9/docs/api/…Hallo
@MarkRotteveel Yes, you're right. My bad.Eudoxia
G
13

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.

Golconda answered 18/5, 2017 at 9:7 Comment(4)
Thank you a lot! Is there any way to automatically include modules?Asiaasian
@Asiaasian Answer of Nicolai seems imply that when the module come out of incubation, you won't need --add-modules.Kumiss
This answer is not correct - --add-modules is not necessary. If it makes a difference whether it is added or not, then it just hides another error - most likely that the developer created module (in this case com.company) is not used on the module path.Degree
@Golconda Thanks for your comment. I had to delete my original one to fix an error I saw too late. I just reposted it.Degree
D
10

Either you or IntelliJ must have made a mistake. You are using an incubator module, about which the documentation says:

Incubator modules are part of the JDK run-time image produced by the standard JDK build. However, incubator modules are not resolved by default for applications on the class path. Applications on the class path must use the --add-modules command-line option to request that an incubator module be resolved. Applications developed as modules can specify requires or requires transitive dependences upon an incubator module directly.

I just confirmed that behavior on java-9-ea+169, i.e. I can compile and launch such a module (from the command line) without additional flags.

The fact that you do not get a compile error seems to indicate that IntelliJ correctly includes the module declaration in the compilation. The fact that you get a run-time error and that this answer helped indicates that the JVM does not see the code you launch as a module.

Degree answered 18/5, 2017 at 13:58 Comment(0)
F
2

I ran into the same problems

java.lang.NoClassDefFoundError: jdk/incubator/http/HttpClient

with java-9-ea+173 and IntelliJ. I followed Eugenes and Nicolais advice to add jdk.incubator.httpclient explicitly to the module path via --add-modules jdk.incubator.httpclient in Run/Debug Configurations (on macOS: Menu Bar -> Run -> Edit Configurations -> Configuration Tab -> VM Options -> --add-modules jdk.incubator.httpclient

enter image description here

After that everything worked fine. Of course you have to add the dependency into the module-info.java like this as said before:

module network {
    requires jdk.incubator.httpclient;
}

UPDATE:

With the latest IntelliJ IDEA 2017.2 EAP 172.2953.9 , I don't need to put the --add-modules to the VM Options. It just works out of the box.

Fanchette answered 13/6, 2017 at 3:12 Comment(2)
My advice was not to add jdk.incubator.httpclient with --add-modules because the documentation and command line experiments show it to be unnecessary. Could be that IntelliJ makes a mistake, though.Degree
Sorry, I misunderstood that. Anyway, the incubator module is loaded if a add the parameter. Maybe it is really an IntelliJ bug. I was also unable to load classes via ServiceLoader with IntelliJ. The just won't be found. On the command line it worked. Either IntelliJ is not yet ready or I am doing something wrong.Fanchette

© 2022 - 2024 — McMap. All rights reserved.