Bug 60041

Summary: NPE in WebappClassLoaderBase
Product: Tomcat 7 Reporter: gehui <gehill_cn>
Component: CatalinaAssignee: Tomcat Developers Mailing List <dev>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: trunk   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X 10.1   
Attachments: patch

Description gehui 2016-08-25 07:36:41 UTC
After deploy war in tomcat, delete the jar in WEB-INF/lib/, then it may throws NullPointerException when load class


The reason as follow:

WebappClassLoaderBase (between line 3093 and 3110 in method openJARs())

    protected boolean openJARs() {
        if (started && (jarFiles.length > 0)) {
            lastJarAccessed = System.currentTimeMillis();
            if (jarFiles[0] == null) {
                for (int i = 0; i < jarFiles.length; i++) {
                    try {
                        jarFiles[i] = new JarFile(jarRealFiles[i]);
                    } catch (IOException e) {
                        if (log.isDebugEnabled()) {
                            log.debug("Failed to open JAR", e);
                        }
                        return false;
                    }
                }
            }
        }
        return true;
    }


If IOException is thrown in line 3099, the jarFiles will be [file1,file2,NULL,NULL,...]. after that, it will return true when invoke openJARs(), beacuse jarFiles[0] != null. 


WebappClassLoaderBase (between line 3271 and 3285 in method openJARs())

            if (openJARs()) {
                for (int i = 0; i < jarFiles.length; i++) {

                    jarEntry = jarFiles[i].getJarEntry(jarEntryPath);

                    if (jarEntry != null) {
                        try {
                            entry.manifest = jarFiles[i].getManifest();
                        } catch (IOException ioe) {
                            // Ignore
                        }
                        break;
                    }
                 }
             }



beacuse jarFiles=[file1,file2,NULL,NULL,...], so jarFiles[i] may be null, This will cause NullPointerException in line 3274. In this case, it is difficult to judge what went wrong.

I attached a patch that fixes it
Comment 1 gehui 2016-08-25 07:39:45 UTC
Created attachment 34171 [details]
patch
Comment 2 Mark Thomas 2016-08-25 21:00:40 UTC
Thanks. I applied a variation that used i18n. Note deleting a JAR like this is expected to break things. This has been fixed in 7.0.x for 7.0.71 onwards.