9

How: To disable Tomcat JARScanner?
Why: To stop Tomcat scan every .jar in my LIB folder.

According to documentation it says that it is possible to disable it within context.xml. But it seems to not be working. (May be I am missing something) I made an exhaustive search in forums and could not find the solution.

This is in context.xml (not working yet):

<JarScanner scanClassPath="false" scanAllFiles="false" scanAllDirectories="false"></JarScanner>

Thanks in advance.

Gonzalo Gallotti
  • 2,413
  • 3
  • 23
  • 28

4 Answers4

14

You should add the JarScanner element as a child of the root Context element in the context.xml file.

I have this kind of META-INF/context.xml file in the war file for disabling the JarScanner:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <JarScanner scanClassPath="false" scanAllFiles="false" scanAllDirectories="false"/>
</Context>
Pang
  • 9,564
  • 146
  • 81
  • 122
Lari Hotari
  • 5,190
  • 1
  • 36
  • 43
  • Is that the global context.xml file located at `/etc/tomcat8/context.xml`? Can jar scanning be disabled on a web-app by web-app basis by instead placing the configuration in your web app's war file in `WEB-INF/web.xml`? – Stephen Ostermiller Jan 12 '18 at 11:19
  • 3
    It can be disabled on web-app by bassis by using application META-INF/context.xml file – Talijanac Feb 08 '18 at 12:17
2

you can disable the JarScanner globally for user-definted patterns by opeining the file at

%TOMCAT_HOME%/conf/catalina.properties

and add a filename pattern to tomcat.util.scan.StandardJarScanFilter.jarsToSkip list. For example, if you want to disable jar scanning completely you could add:

tomcat.util.scan.StandardJarScanFilter.jarsToSkip=\
*.jar,\

NOTE: this may of course lead to issues if you're employing JSTL, as templates won't be found by the scanner

Black
  • 5,023
  • 6
  • 63
  • 92
1

in your java app add this :

@Bean
public TomcatServletWebServerFactory tomcatServletFactory() {
    return new TomcatServletWebServerFactory() {
        @Override
        protected void postProcessContext(final Context context) {
            ((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
        }
    };
}
Shaheed
  • 92
  • 4
1

This is what I did for Spring Boot.

Basically, append a new ignored jar file to the existing list of jars to ignore. This way, you don't totally disable the scanner, affecting who knows what else.

@Configuration
public class Config {

    @Bean
    public ServletWebServerFactory servletContainer() {
        return new TomcatServletWebServerFactory() {
            @Override
            protected void postProcessContext(Context context) {
                // db2 puts a ref to pdq.jar in the manifest, and tomcat then tries to find it, but it doesn't exist.
                // The jar isn't needed, so we just disable looking for it. You could also remove it from the manifest,
                // but that prob needs to be done after the build process.
                JarScanFilter jarScanFilter = context.getJarScanner().getJarScanFilter();
                if (jarScanFilter instanceof StandardJarScanFilter) {
                    StandardJarScanFilter filter = (StandardJarScanFilter) jarScanFilter;
                    String oldTldSkip = filter.getTldSkip();
                    String newTldSkip = oldTldSkip == null || oldTldSkip.trim().isEmpty() ? "pdq.jar" : oldTldSkip + ",pdq.jar";
                    filter.setTldSkip(newTldSkip);
                } else {
                    logger.warn("Unable to disable the tomcat jar scanner for pdq.jar. You may see a FileNotFound exception complaining of not finding a db2 pdq.jar file. You can probably ignore the error. Ref: https://stackoverflow.com/questions/11656596/how-to-disable-tomcat-jarscanner");
                }
            }
        };
    }

}
goat
  • 31,486
  • 7
  • 73
  • 96