springboot启动嵌入式tomcat报错找不到jar包,关键字:FileNotFoundException,derbyLocale_cs.jar,StandardJarScanner.scan
异常:
java.io.FileNotFoundException: /Users/lanhuajian/.m2/repository/org/apache/derby/derby/10.13.1.1/derbyLocale_cs.jar (No such file or directory)
at java.util.zip.ZipFile.open(Native Method) ~[?:1.8.0_152]
at java.util.zip.ZipFile.<init>(ZipFile.java:225) ~[?:1.8.0_152]
at java.util.zip.ZipFile.<init>(ZipFile.java:155) ~[?:1.8.0_152]
at java.util.jar.JarFile.<init>(JarFile.java:166) ~[?:1.8.0_152]
at java.util.jar.JarFile.<init>(JarFile.java:130) ~[?:1.8.0_152]
at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:60) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:338) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:288) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.jasper.servlet.TldScanner.scanJars(TldScanner.java:262) [tomcat-embed-jasper-8.5.23.jar:?]
at org.apache.jasper.servlet.TldScanner.scan(TldScanner.java:104) [tomcat-embed-jasper-8.5.23.jar:?]
at org.apache.jasper.servlet.JasperInitializer.onStartup(JasperInitializer.java:101) [tomcat-embed-jasper-8.5.23.jar:?]
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5196) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) [tomcat-embed-core-8.5.23.jar:8.5.23]
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) [tomcat-embed-core-8.5.23.jar:8.5.23]
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [?:1.8.0_152]
at java.util.concurrent.FutureTask.run(FutureTask.java) [?:1.8.0_152]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_152]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_152]
at java.lang.Thread.run(Thread.java:748) [?:1.8.0_152]
springboot集成hadoop等组件后会报这个错,一般和hadoop集成需要把老版本的sevlet、jsp、tomcat依赖去掉,但是我去掉之后还是存在。
所以我跟踪了springboot初始化tomcat的代码,发现在TomcatEmbeddedServletContainerFactory#prepareContext()
有个tldSkipPatterns
的东西, 就心想这个是不是会告诉tomcat不要扫描这些jar包呢?试了一下,真的可以!
因此可以得出如下解决方案,在初始化TomcatEmbeddedServletContainerFactory
后,在后置bean处理器中给它加上tldSkipPatterns即可
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Arrays;
import java.util.List;
import static java.util.stream.Collectors.toList;
@Configuration
public class TomcatContainerConfig {
@Value("${tldSkipPatterns}")
private String[] tldSkipPatterns;
@Bean
public BeanPostProcessor TomcatContainerPostProcessor() {
List<String> notEmptyTldSkipPatterns = Arrays.stream(tldSkipPatterns)
.filter(tldSkipPattern -> !tldSkipPattern.trim().isEmpty())
.collect(toList());
return new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (beanName.equals("tomcatEmbeddedServletContainerFactory") &&
bean instanceof TomcatEmbeddedServletContainerFactory) {
TomcatEmbeddedServletContainerFactory factory = (TomcatEmbeddedServletContainerFactory) bean;
if (!notEmptyTldSkipPatterns.isEmpty()) {
factory.addTldSkipPatterns(notEmptyTldSkipPatterns.toArray(new String[0]));
}
}
return bean;
}
};
};
}
增加下面的配置到 application.properties
:
tldSkipPatterns=derbyLocale_*.jar,jaxb-api.jar,jsr173_1.0_api.jar,jaxb1-impl.jar,activation.jar