-1

My application works fine with JDK 8 running this snipper code below

import javax.net.ssl.HttpsURLConnection;
import sun.net.www.protocol.https.Handler;

final URL resourceUrl = new URL(null, builder.addToURI(uri).toString(), new sun.net.www.protocol.https.Handler());

Now, I am upgrading to JDK 17, and I get the error message. It is caused by the package sun.net.www.protocol.https.Handler is already moved to java.base (internal package)

Package 'sun.net.www.protocol.https' is declared in module 'java.base', which does not export it to the unnamed module

I know there is a workaround solution that uses --add-exports <module>/<package>=<target-module>(,<target-module>)* command, but I cannot use it for production, at least in my case.

Is there a way that can replace this internal call?

NoName
  • 877
  • 12
  • 28
  • 1
    Upgrade to a proper HTTP library that doesn't rely on Java internal classes – Jorn Aug 24 '23 at 10:53
  • 7
    You shouldn't have used a `sun.*` class to start with. Relying on internal classes that may change or be removed is not smart. The parameter type for `URL` is `URLStreamHandler`, you just used an internal class because of reasons unknown. – Kayaman Aug 24 '23 at 11:25
  • Why do you need it? Start from your needs and we'll explain how to di it with a proper http Library. also https://stackoverflow.com/questions/65206823/how-to-update-and-avoid-sun-net-www-protocol-https-handler-is-internal-propriet – pdem Aug 24 '23 at 12:07
  • 3
    https:… URLs work just fine without any handler arguments. The passing of that argument is probably a holdover from the 1990s. – VGR Aug 24 '23 at 14:48
  • 1
    Maybe the result of bad answers like [this one](https://stackoverflow.com/a/12796611/2711488) It doesn’t even have the desired effect. Some pre-Java 6 code might require the class `com.sun.net.ssl.HttpsURLConnection`, but to work-around this, you’d need the handler from the `com.sun.net.ssl…` package hierarchy, not the `sun.net…` one. This legacy support has been removed around Java 13, as it seems. But, as said, it’s unrelated to the `sun.net.www.protocol.https.Handler` which doesn’t need be be accessed directly. – Holger Aug 24 '23 at 16:31
  • That snippet code in my application supports https with SSL, if I remove the handler parameter from the URL constructor, I will get some issues when using HTTPs protocol – NoName Aug 25 '23 at 03:23
  • More info, the handler parameter was added to the URL constructor to handle SSL certificates. – NoName Aug 25 '23 at 03:31
  • 2
    Just get rid of `new sun.net.www.protocol.https.Handler()`. That just asserts the default. It never did anything useful. If you then get issues, post them. XY problem. – user207421 Aug 25 '23 at 05:30

1 Answers1

2

Is it not working if you remove it entirely?

final URL resourceUrl = new URL(builder.addToURI(uri).toString());

Code to test HTTPS

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class UrlTest {
    public static void main(String args[]) throws Exception {
        final URL resourceUrl = new URL("https://stackoverflow.com/questions/76968545/how-to-replace-sun-net-www-protocol-https-handler-in-jdk-17");
        BufferedReader in = new BufferedReader(new InputStreamReader(resourceUrl.openStream()));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
    }
}
nosaku
  • 70
  • 7
  • If I remove the ```handler``` parameter from the URL constructor, I will get an error when using HTTPS URLs. That ```handler``` was added to support both HTTP and HTTPS URLs in my application (and it works on JDK 8) – NoName Aug 25 '23 at 03:24
  • More info, the ```handler``` parameter was added to the URL constructor to handle SSL certificates. – NoName Aug 25 '23 at 03:30
  • You may have to add any custome certificates separately. I just tested simple test code and it works fine. If you are using any self signed certificates or so then they need to be added to the certificate store. – nosaku Aug 25 '23 at 05:17
  • 3
    @NoName You will get *what* error? – user207421 Aug 25 '23 at 05:30
  • 2
    Simplified example: `new URL("https://stackoverflow.com/questions/76968545") .openStream().transferTo(System.out);` @NoName as said in the comments on your question, the handler you are specifying, is the default. If specifying it explicitly makes a difference, your environment must deviate from the default and the better fix is to remove the option (or whatever cause) from your environment to return to the default behavior. And one gentle reminder: you need a solution that works with JDK 17, not one that happened to work with JDK 8 but doesn’t work with JDK 17. – Holger Aug 25 '23 at 13:33
  • When I review the source code, it uses ```sun.net.www.protocol.https.Handler()``` to support proxy with SSL (https) requests on the SAP PI/PO platform. And, on the new version of my application, I don't support proxy anymore. So, removing the handler from the URL constructor is an acceptable answer. – NoName Aug 28 '23 at 08:53