Java 9 no class definition exception

后端 未结 3 1358
隐瞒了意图╮
隐瞒了意图╮ 2020-12-19 00:57

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) {
          


        
相关标签:
3条回答
  • 2020-12-19 01:18

    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

    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.

    0 讨论(0)
  • 2020-12-19 01:24

    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.

    0 讨论(0)
  • 2020-12-19 01:25

    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.

    0 讨论(0)
提交回复
热议问题