Search code examples
javajava-17

How to replace sun.net.www.protocol.https.Handler in JDK 17?


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?


Solution

  • 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);
            }
        }
    }