vlambda博客
学习文章列表

禁用springboot内嵌tomcat的MANIFEST扫描

    本文介绍实现一个将word转为pdf的需求过程中,遇到jar扫描异常的问题排查及解决。本文采用springboot2.0集成jodconverter,使用openoffice6。

    首先添加依赖,

<dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-spring-boot-starter</artifactId> <version>4.2.2</version></dependency>
<dependency> <groupId>org.jodconverter</groupId> <artifactId>jodconverter-local</artifactId> <version>4.2.2</version></dependency>

修改[配置文件application.yml](https://github.com/sbraconnier/jodconverter/blob/master/jodconverter-samples/jodconverter-sample-spring-boot/src/main/resources/application.yml)


@Autowired private DocumentConverter converter;

格式转换:

converter.convert(new File(wordFilePath)).to(targetFile).as(DefaultDocumentFormatRegistry.HTML).execute();


[参考示例ConverterController.java](https://github.com/sbraconnier/jodconverter/blob/master/jodconverter-samples/jodconverter-sample-spring-boot/src/main/java/org/jodconverter/sample/springboot/ConverterController.java)


虽然能够正常转换成pdf文件,但是出现一些警告信息,终归不爽。提示信息如下:

2021-05-18 08:52:43.929 WARN [-,,,] 11888 --- [ost-startStop-1] o.a.tomcat.util.scan.StandardJarScanner : Failed to scan [file:/D:/repository/org/openoffice/juh/4.1.2/ridl.jar] from classloader hierarchy
java.io.FileNotFoundException: D:\repository\org\openoffice\juh\4.1.2\ridl.jar (系统找不到指定的文件。) at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:225) at java.util.zip.ZipFile.<init>(ZipFile.java:155) at java.util.jar.JarFile.<init>(JarFile.java:166) at java.util.jar.JarFile.<init>(JarFile.java:130) at org.apache.tomcat.util.compat.JreCompat.jarFileNewInstance(JreCompat.java:197) at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:65) at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49) at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:385) at org.apache.tomcat.util.scan.StandardJarScanner.processURLs(StandardJarScanner.java:320) at org.apache.tomcat.util.scan.StandardJarScanner.doScanClassPath(StandardJarScanner.java:271) at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:234) at org.apache.jasper.servlet.TldScanner.scanJars(TldScanner.java:262) at org.apache.jasper.servlet.TldScanner.scan(TldScanner.java:104) at org.apache.jasper.servlet.JasperInitializer.onStartup(JasperInitializer.java:101) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
2021-05-18 08:52:43.929 WARN [-,,,] 11888 --- [ost-startStop-1] o.a.tomcat.util.scan.StandardJarScanner : Failed to scan [file:/D:/repository/org/openoffice/juh/4.1.2/jurt.jar] from classloader hierarchy
java.io.FileNotFoundException: D:\repository\org\openoffice\juh\4.1.2\jurt.jar (系统找不到指定的文件。) at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:225) at java.util.zip.ZipFile.<init>(ZipFile.java:155) at java.util.jar.JarFile.<init>(JarFile.java:166) at java.util.jar.JarFile.<init>(JarFile.java:130) at org.apache.tomcat.util.compat.JreCompat.jarFileNewInstance(JreCompat.java:197) at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:65) at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49) at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:385) at org.apache.tomcat.util.scan.StandardJarScanner.processURLs(StandardJarScanner.java:320) at org.apache.tomcat.util.scan.StandardJarScanner.doScanClassPath(StandardJarScanner.java:271) at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:234) at org.apache.jasper.servlet.TldScanner.scanJars(TldScanner.java:262) at org.apache.jasper.servlet.TldScanner.scan(TldScanner.java:104) at org.apache.jasper.servlet.JasperInitializer.onStartup(JasperInitializer.java:101) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
2021-05-18 08:52:43.930 WARN [-,,,] 11888 --- [ost-startStop-1] o.a.tomcat.util.scan.StandardJarScanner : Failed to scan [file:/D:/repository/org/openoffice/jurt/4.1.2/ridl.jar] from classloader hierarchy
java.io.FileNotFoundException: D:\repository\org\openoffice\jurt\4.1.2\ridl.jar (系统找不到指定的文件。) at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:225) at java.util.zip.ZipFile.<init>(ZipFile.java:155) at java.util.jar.JarFile.<init>(JarFile.java:166) at java.util.jar.JarFile.<init>(JarFile.java:130) at org.apache.tomcat.util.compat.JreCompat.jarFileNewInstance(JreCompat.java:197) at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:65) at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49) at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:385) at org.apache.tomcat.util.scan.StandardJarScanner.processURLs(StandardJarScanner.java:320) at org.apache.tomcat.util.scan.StandardJarScanner.doScanClassPath(StandardJarScanner.java:271) at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:234) at org.apache.jasper.servlet.TldScanner.scanJars(TldScanner.java:262) at org.apache.jasper.servlet.TldScanner.scan(TldScanner.java:104) at org.apache.jasper.servlet.JasperInitializer.onStartup(JasperInitializer.java:101) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)
2021-05-18 08:52:43.930 WARN [-,,,] 11888 --- [ost-startStop-1] o.a.tomcat.util.scan.StandardJarScanner : Failed to scan [file:/D:/repository/org/openoffice/jurt/4.1.2/unoloader.jar] from classloader hierarchy
java.io.FileNotFoundException: D:\repository\org\openoffice\jurt\4.1.2\unoloader.jar (系统找不到指定的文件。) at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:225) at java.util.zip.ZipFile.<init>(ZipFile.java:155) at java.util.jar.JarFile.<init>(JarFile.java:166) at java.util.jar.JarFile.<init>(JarFile.java:130) at org.apache.tomcat.util.compat.JreCompat.jarFileNewInstance(JreCompat.java:197) at org.apache.tomcat.util.scan.JarFileUrlJar.<init>(JarFileUrlJar.java:65) at org.apache.tomcat.util.scan.JarFactory.newInstance(JarFactory.java:49) at org.apache.tomcat.util.scan.StandardJarScanner.process(StandardJarScanner.java:385) at org.apache.tomcat.util.scan.StandardJarScanner.processURLs(StandardJarScanner.java:320) at org.apache.tomcat.util.scan.StandardJarScanner.doScanClassPath(StandardJarScanner.java:271) at org.apache.tomcat.util.scan.StandardJarScanner.scan(StandardJarScanner.java:234) at org.apache.jasper.servlet.TldScanner.scanJars(TldScanner.java:262) at org.apache.jasper.servlet.TldScanner.scan(TldScanner.java:104) at org.apache.jasper.servlet.JasperInitializer.onStartup(JasperInitializer.java:101) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1412) at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1402) at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) at java.util.concurrent.FutureTask.run(FutureTask.java) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) at java.lang.Thread.run(Thread.java:748)

根据提示信息找到类StandardJarScanner,定位到

 if (isScanManifest()) { processManifest(jar, isWebapp, classPathUrlsToProcess); }


打开文件juh-4.1.2.jar

\META-INF\MANIFEST.MF

Manifest-Version: 1.0Class-Path: ridl.jar jurt.jar ../../lib/ ../bin/Solar-Version: 412m3(Build:9782)Sealed: trueRegistrationClassName: com.sun.star.comp.JavaUNOHelperServicesUNO-Type-Path:

果然是扫描并尝试加载了MANIFEST.MF的Class-Path。

isScanManifest()通过属性scanManifest来控制是否扫描Manifest。

设置禁用扫描清单文件,

Spring Boot 1.x:

@Bean public EmbeddedServletContainerFactory embeddedServletContainerFactory() { return new TomcatEmbeddedServletContainerFactory() { @Override protected void postProcessContext(Context context) { ((StandardJarScanner) context.getJarScanner()).setScanManifest(false); } }; }

Spring Boot 2.0:

 @Bean public TomcatServletWebServerFactory tomcatFactory() { return new TomcatServletWebServerFactory() { @Override protected void postProcessContext(Context context) { ((StandardJarScanner) context.getJarScanner()).setScanManifest(false); } }; }