JDK-8225037 : java.net.JarURLConnection::getJarEntry() throws NullPointerException
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.net
  • Affected Version: 9,10,11,12,13
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2019-05-30
  • Updated: 2020-09-02
  • Resolved: 2019-06-05
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 11 JDK 13 JDK 14
11.0.10-oracleFixed 13 b24Fixed 14Fixed
Description
Spec says: "Return the JAR entry object for this connection, if any. This method returns null if the JAR file URL corresponding to this connection points to a JAR file and not a JAR file entry."

However getJarEntry() throws NullPointerException instead of returning null in below scenario:

public static void main(String[] args) throws Exception {
        //please change below url accordingly
        URL url = new URL("jar:file:/D:/sample.jar!/");
        JarURLConnection jarURLConnection = new CustomJarURLConnection(url);
        /**
         * Spec says:
         *
         * Return the JAR entry object for this connection, if any. This
         * method returns null if the JAR file URL corresponding to this
         * connection points to a JAR file and not a JAR file entry.
         *
         * As "url" points to a JAR file and not a JAR file entry, hence getJarEntry() must return null.
         * However it throws null pointer exception.
         *
         **/
        JarEntry jarEntry = jarURLConnection.getJarEntry();
        System.out.println("This should be printed, however above line throws NullPointerException");
    }

    static class CustomJarURLConnection extends JarURLConnection {
        private URL pJarFileURL;
        protected CustomJarURLConnection(URL url) throws MalformedURLException {
            super(url);
            pJarFileURL = url;
        }

        @Override
        public JarFile getJarFile() throws IOException {
            return ((JarURLConnection)pJarFileURL.openConnection()).getJarFile();
        }

        @Override
        public void connect() throws IOException {}
    }
}
Comments
There is a JCK issue and that will require a challenge to see if it can be excluded.
02-09-2020

I think this should be ok, given we don't see a TCK violation. But obviously there are no related items so it's an indication that it's unproblematic. Will monitor our TCK results after this got pushed. Approving.
04-08-2020

Fix Request (11u) This fixes the specification violation (the Javadoc is the same in 11u). Patch applies cleanly to 11u, new test fails without the patch, passes with it. Additional tier{1,2} testing passes with the patch.
04-08-2020

The implementation of java.net.JarURLConnection has a private String, `entryName`, that is used to hold the optional jar file entry name. If the JAR file URL corresponding to the JarURLConnection points to a JAR file and not a JAR file entry, then `entryName` will be `null` ( which should be ok ). The default implementation of JarURLConnection::getJarEntry should only invoke `getJarEntry(entryName)` if the name is non-null. ( This is not an issue in the built-in JarURLConnection handler implementation, as it already correctly handles a null `entryName` ) Review thread: https://mail.openjdk.java.net/pipermail/net-dev/2019-May/012710.html
31-05-2019