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

后端 未结 2 691
南旧
南旧 2020-12-17 22:50

I am trying to use the HttpClient from incubator in Java 9 maven project. I am not getting any Compilation issue. The project builds successfully. But when I try to run the

相关标签:
2条回答
  • 2020-12-17 23:25

    The difference during the execution is using the classpath vs the modulepath. The statement in the JEP11#Incubator Modules to notice is as

    ... incubator modules are not resolved by default for applications on the class path.

    so in order to execute the code using the classpath

    Applications on the class path must use the --add-modules command-line option to request that an incubator module be resolved.


    If you want to execute without using the --add-modules option, while creating a new module and trying to execute the Main class make sure the java command executed using the module path using the arguments :

    -p or --module-path <module path>
    

    Searches for directories from a semicolon-separated (;) list of directories. Each directory is a directory of modules.

    -m or --module <module>/<mainclass
    

    Specifies the name of the initial module to resolve and, if it isn’t specified by the module, then specifies the name of the mainclass to execute.

    The complete command would be something (e.g.) like :

    .../jdk-9.0.1.jdk/Contents/Home/bin/java 
           -p .../jdk9-httpincubate-maven/target/classes 
           -m jdk.httpincubate.maven/http2.Main
    

    Note:

    • The above directory structure is followed in the sample project that I have created on GitHub to replicate the same as well.

    • Just to add to it and without any publicisizing I am using 2017.3 EAP of intelliJ and it lets me Run the Main class without the VM arguments as well (using the command including the arguments as shared above.)

    0 讨论(0)
  • 2020-12-17 23:47

    If the accepted answer still did not work for you then..

    The missing secret is to also include the maven-surefire-plugin with the same compiler arg.

    <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                    <configuration>
                        <source>10</source>
                        <target>10</target>
                        <compilerArgument>--add-modules=jdk.incubator.httpclient</compilerArgument>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.21.0</version>
                    <configuration>
                        <argLine>--add-modules=jdk.incubator.httpclient</argLine>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    0 讨论(0)
提交回复
热议问题