How to disable Tomcat JARScanner
Asked Answered
B

4

10

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.

Bestride answered 25/7, 2012 at 18:51 Comment(4)
Please post your attempted configuration. It's hard to guess why it's not working when you don't post what you've tried.Flagstad
I've added the line i'am using in context.xml. Sorry and thanksBestride
Do you want to disable scanning of all JAR files entirely? You have no .tld files that need to be auto-discovered and you don't use any annotations?Flagstad
I removed the entry from web.xml and the issue stoped. Still wanting to disable JarScanner from Context.xml (for prototyiping purposes)Bestride
P
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>
Pavlish answered 15/1, 2015 at 22:24 Comment(2)
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?Ramshackle
It can be disabled on web-app by bassis by using application META-INF/context.xml fileVadnee
D
3

you can disable the JarScanner globally for user-defined patterns by opening 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

Dryfoos answered 30/5, 2020 at 11:54 Comment(0)
B
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);
        }
    };
}
Bemis answered 4/4, 2022 at 9:55 Comment(0)
L
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://mcmap.net/q/1061372/-how-to-disable-tomcat-jarscanner");
                }
            }
        };
    }

}
Llamas answered 27/8, 2022 at 19:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.