0

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();
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Nurul
  • 13
  • 7
  • Check this out: https://stackoverflow.com/questions/66676851/java-net-http-does-not-exist-error-on-jdk11 – AlirezaAsadi Jan 03 '22 at 20:43
  • have you tried this answer in the question you linked? https://stackoverflow.com/questions/36910746/package-java-net-http-does-not-exist-error-on-jdk9/43111760#43111760 – Tschallacka Jan 03 '22 at 20:44
  • Does this answer your question? ["package java.net.http does not exist" error on JDK9](https://stackoverflow.com/questions/36910746/package-java-net-http-does-not-exist-error-on-jdk9) – AlirezaAsadi Jan 03 '22 at 20:46
  • what class are you trying to use? from where you get the error? please add information on how to reproduce this issue. – eis Jan 03 '22 at 20:51
  • @alirezafnatica yes, already checked that out. My 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) meanwhile my android studio is the latest version. – Nurul Jan 03 '22 at 23:55
  • @Tschallacka yup already tried, as per in that answer, incubator has been removed since 9, mine is 11. I added the "import java.net.http.HttpClient;" successfully. only failed when building it. not sure why. – Nurul Jan 03 '22 at 23:58
  • @eis adding code. Basically I just want to do http request. I added java.net.http jar file as dependency and my code recognize it. But somehow when building the app, it thrown "package java.net.http is not exists" – Nurul Jan 04 '22 at 00:04
  • So ... are you trying to build an app to run on Android? Or to run on a Java SE platform? In the former case ... why are you using `java.net.http` which is not an Android native API? In the latter case, why are you using Android Studio and why do you have dependencies on androidx, etcetera? – Stephen C Jan 04 '22 at 01:13
  • If you are building for Android and *"just want to do an HTTP request"* is your complete list of relevant requirements, then I suggest using a Android native API; e.g. [volley](https://developer.android.com/training/volley). – Stephen C Jan 04 '22 at 01:24
  • @StephenC however, I read Volley wasn't suitable for streaming. This app intent to streaming live video; from android phone to web server. – Nurul Jan 04 '22 at 06:03
  • 1
    “I downloaded the jar” what jar? From where? – Holger Jan 04 '22 at 10:58
  • @Holger I downloaded jdk11 and get java.net.http.jmods, change jmods to jar then add dependency to that jar file – Nurul Jan 04 '22 at 11:06
  • 2
    A jmod is not a jar file. Both are zip containers, but they have a different directory structure. – Holger Jan 04 '22 at 12:49

2 Answers2

0

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.

Nurul
  • 13
  • 7
0

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>
IanB
  • 1