简体   繁体   中英

package java.net.http does not exist for JDK 11

I don't know what's wrong as there is no error in my code but build failed with error package java.net.http does not exist. I downloaded the jar and added it as dependency. It also listed in java --list-modules.

  • compileSdkVersion 32
  • buildToolsVersion '30.0.2'
  • minSdkVersion 26
  • targetSdkVersion 30
  • gradleVersion = '7.2'
  • sourceCompatibility
  • JavaVersion.VERSION_11
  • targetCompatibility JavaVersion.VERSION_11
  • implementation files('libs/java.net.http.jar')

The jar I got from jmods folder in jdk folder. I tried update to jdk 17 also same. I try solution from this "package java.net.http does not exist" error on JDK9 but return module not found. My impacted code

import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

HttpRequest request = HttpRequest.newBuilder()
                    .uri(new URI(reqURI))
                    .build();

HttpResponse<String> response = httpClient.send(request, 
HttpResponse.BodyHandlers.ofString());
System.out.println("uri:" + response.uri());
System.out.println("header:" +response.headers());
System.out.println("body" + response.body());
System.out.println("status code" + response.statusCode());

java -version:

  • openjdk version "11" 2018-09-25
  • OpenJDK Runtime Environment 18.9 (build 11+28)
  • OpenJDK 64-Bit Server VM 18.9 (build 11+28, mixed mode)

Inside build.gradle

dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.google.firebase:firebase-messaging:23.0.0'
implementation files('libs/simple-xml-2.6.7/simple-xml-2.6.7.jar')
implementation files('libs/ksoap2-android-assembly-2.6.5/ksoap2-android-assembly-2.6.5.jar')
implementation files('libs/org-apache-commons-io/org-apache-commons-io.jar')
implementation files('libs/sc-light-jdk15on-1.47.0.2.jar')
implementation files('libs/scpkix-jdk15on-1.47.0.2.jar')
implementation files('libs/scprov-jdk15on-1.47.0.2.jar')
implementation files('libs/java.net.http.jar')
}

Actually, what I want is to replace below code which already deprecated

 DefaultHttpServerConnection conn = new DefaultHttpServerConnection();
params = new BasicHttpParams();

params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
        .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE,8*1024)
        .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,false)
        .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
        .setParameter(CoreProtocolPNames.ORIGIN_SERVER,"HttpComponents/1.1");
conn.bind(socket, params);

Extracting the information from the requested URI

HttpRequest request = conn.receiveRequestHeader();
request.getRequestLine().getMethod().toUpperCase(Locale.ENGLISH);
Uri uri = Uri.parse(request.getRequestLine().getUri());
String httpAPI = uri.getPath();
String httpQuery = uri.getQuery();

I solved the problem by change the app's targetSdkVersion and CompileSdkVersion (refer to Migrating apps to Android 11 ), else there'll be a lot of deprecated functions.

For those using maven remember to upgrade the release # in pom.xml

I had the same symptom [ERROR]... .java:[5,21] package java.net.http does not exist [ERROR]... .java:[144,27] package HttpRequest does not exist Upgraded from Java 16 to 18, updated JAVA_HOME, rebooted - no joy Realized the maven pom.xml might be outdated - it was release 10, updated release # to match JDK 18 Solved, now compiles fine.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.10.1</version>
    <inherited>true</inherited>
    <configuration>
        <release>18</release>
    </configuration>
</plugin>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM