Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8225037

java.net.JarURLConnection::getJarEntry() throws NullPointerException

    XMLWordPrintable

Details

    • b24

    Backports

      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 {}
            }
        }

        Attachments

          Issue Links

            Activity

              People

                chegar Chris Hegarty
                dsingh Durgesh Singh (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                5 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: