Search code examples
javamigrationjava-11java-6

Migration code from 1.6 jdk to 11 jdk problem with sun.net.www.protocol.file


I have a problem with migration old code which used sun.net.* and sun.net.www.protocol.file.FileURLConnection Ant cannot build project on jdk 11 because FileURLConnection is from version 8/9 internal class of base package. FileURLCOnnection was use to check instance of returned class so I will need good idea how I can replace this fragment without destroing logic (fragment belowe).

connection instanceof FileURLConnection
    for (URL url = null; resources.hasMoreElements()
                && ((url = resources.nextElement()) != null); ) {
                try {
                    connection = url.openConnection();

                    if (connection instanceof JarURLConnection) {
                        checkJarFile((JarURLConnection) connection, pckgname,
                            classes);
                    } else if (connection instanceof FileURLConnection) {
                        try {
                            checkDirectory(
                                new File(URLDecoder.decode(url.getPath(),
                                    "UTF-8")), pckgname, classes);
                        } catch (final UnsupportedEncodingException ex) {
                            throw new ClassNotFoundException(
                                pckgname
                                    + " does not appear to be a valid package (Unsupported encoding)",
                                ex);
                        }
                    } else
                        throw new ClassNotFoundException(pckgname + " ("
                            + url.getPath()
                            + ") does not appear to be a valid package");
                } catch (final IOException ioex) {
                    throw new ClassNotFoundException(
                        "IOException was thrown when trying to get all resources for "
                            + pckgname, ioex);
                }
            }

Solution

  • You should be able to replace:

    connection instanceof FileURLConnection
    

    By:

    "file".equals(url.getProtocol())