0

I try to learn using HTTPClient. But HttpClient client = HttpClient.newHttpClient(); does not work.

In IntelliJ I've updated my compiler to use Java version 12 and in the pom.xml I've set java.version property to 12.

After I write HTTPClient it gets import sun.net.www.http.HttpClient, and then newHttpClient() method does not work. For this method I need import java.net.http.HttpClient;

This is my pom.xml:

<properties>
    <java.version>12</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.8</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
daniel
  • 2,665
  • 1
  • 8
  • 18
Gioli
  • 55
  • 9
  • When importing a specific class, in IntelliJ you have the option to specify which package to import. Just select the one from `java.net.http` – Alexandru Somai Mar 02 '20 at 09:44
  • 3
    So... what is stopping you from editing the import statement so that it is correct? – Gimby Mar 02 '20 at 09:47
  • Read this https://mkyong.com/java/java-11-httpclient-examples/ – Prog_G Mar 02 '20 at 09:50
  • @AlexandruSomai I know but I have no option to import right package. It imports sun.net.www.http.HttpClient; – Gioli Mar 02 '20 at 10:18
  • Import statements are just code, you can edit it yourself. What happens when you change the import statement to the correct one by hand? – Gimby Mar 02 '20 at 10:30
  • The IDE you are using auto imports the class he thinks you want. But in this case it is wrong, so fix it manually. Delete that line: *import sun.net.www.http.HttpClient;*, and add this line: *import java.net.http.HttpClient;* – Bentaye Mar 02 '20 at 10:43
  • your IDE is importing the old package which is not supported anymore (was OK for Java 8, I believe it was removed in Java 11) - I do not know IntelliJ but I think there must some place to configure your project and set which version of java to use (which standard lib to use - try [How to set IntelliJ IDEA Project SDK](https://stackoverflow.com/q/16765726/85421)) – user85421 Mar 02 '20 at 10:53
  • Has IntelliJ created a module-info.java file for you? If it has then you need to add: `requires java.net.http;` inside to read the `java.net.http` module that exports the `java.net.http` package. – daniel Mar 02 '20 at 17:16
  • I want to use HTTPClient in java 8. So how can I use it? The solution should compatible with java 8 and java 11 also – Bhavin S. Jan 13 '21 at 12:59

2 Answers2

0

It works now. I installed java Version 13 on my comuter and edites in Project Structure

Gioli
  • 55
  • 9
-2

You should add this dependency to your POM.xml

 <dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>$version</version>
 </dependency>
  • 2
    Wrong. This is about he HttpClient which is built into the JDK itself, not the Apache HttpClient. – Gimby Mar 02 '20 at 10:28