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