Unable to use Java 9's HttpClient in a SpringBoot-Maven server

0

I wanted to use Java 9 's HttpClient in my SpringBoot application. So I wrote the code for HttpClient in IntelliJ in a Maven project. The packages/module jdk.incubator.httpclient.* was automatically included.

When I tried to run the application, it threw a compilation error saying

"package jdk.incubator.httpclient is declared in module jdk.incubator.httpclient, which is not in the module graph".

So I researched a little bit on the internet and a got a clue that creating a module file (module-info.java) saying something like this :

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

would resolve the issue. So I did that, but other packages which were imported in other classes were now unresolved ( it could be resolved by adding its module to this class but adding all of them would be a painful task and it would be a repetition of what was already included in pom.xml file).

So I researched a little bit more, and I got a clue that adding this ::

<compilerArgument>--add-modules=jdk.incubator.httpclient</compilerArgument>

to the configuration section in maven-compiler-plugin of pom.xml would resolved the issue.

After taking the last step the project compiled successfully but when I tried to run the application it threw a runtime error saying ,

java.lang.ClassNotFoundException: jdk.incubator.http.HttpRequest

and

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

I tried to look for solutions online but was not able to find it, one person faced the same issue , he got it partially resolved using the last step but got the same runtime error as me, he asked for the solution but did not receive any.

Is it possible to run HttpClient , HttpRequest in SpringBoot-Maven application, if yes , then how ? Please guide.

lakhansoren

Posted 2019-09-13T12:52:11.450

Reputation: 1

In the end I had to use HttpURLConnection class for my purpose. – lakhansoren – 2019-09-13T12:52:52.750

Answers

0

I solved the problem in the following way.

  1. I updated my java to openjdk 12.
  2. From java 11 onwards HttpClient , HttpRequest and related classes belongs to java.net package.
  3. I updated my intelliJ language level in project structure to 12.
  4. Removed module-info.java file.
  5. Removed the compiler-argument in pom.xml

Then I successfully compiled and ran it. It successfully got an HttpResponse.

lakhansoren

Posted 2019-09-13T12:52:11.450

Reputation: 1