无法使用主线程的上下文类加载器将Web应用程序添加到@SpringBootApplication的内置Tomcat中

时间:2017-02-12 18:55:15

标签: java spring tomcat contextclassloader

我正在尝试加载war作为我随独立jar附带的网络应用程序。当我的@SpringBootApplication启动时,我提取war文件并将其复制到文件系统。在那之后,我做了一个简单的tomcat.addWebapp()电话,我希望它可以工作。但看看:

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {

    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {

            String appHome = System.getProperty(Environment.APP_HOME);

            String targetFileName = "web-0.0.1-SNAPSHOT.war";               
            InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(targetFileName);

            File target = new File(Paths.get(appHome, targetFileName).toString());

            try {

                // Copy the file to the file system
                java.nio.file.Files.copy(resourceAsStream, target.getAbsoluteFile().toPath(), StandardCopyOption.REPLACE_EXISTING);

                // Add the web application and bind it to the context path /web
                Context context = tomcat.addWebapp("/web", target.getAbsolutePath());
                WebappLoader loader = new WebappLoader(Thread.currentThread().getContextClassLoader());
                context.setLoader(loader);

            } catch (ServletException ex) {
                throw new IllegalStateException("Failed to add webapp", ex);
            } catch(Exception e) {                  
                throw new IllegalStateException("Failed to add webapp", e);
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }
    };
}

问题是我在FileNotFoundException返回后立即获得 servletContainerFactory()。您可以看到完整输出here,但归结为:

java.io.IOException: Unable to create the directory [/tmp/tomcat.2878705033245220680.8080/webapps/web]
    at org.apache.catalina.startup.ExpandWar.expand(ExpandWar.java:115) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.startup.ContextConfig.fixDocBase(ContextConfig.java:615) [tomcat-embed-core-8.5.11.jar:8.5.11]
..
2017-02-12 19:39:44.393  INFO 10137 --- [ost-startStop-1] o.apache.catalina.startup.ContextConfig  : No global web.xml found
2017-02-12 19:39:44.590  WARN 10137 --- [ost-startStop-1] o.a.tomcat.util.scan.StandardJarScanner  : Failed to scan [file:/home/stefan/.m2/repository/org/bytedeco/javacv/1.3/javacpp-1.3.jar] from classloader hierarchy
..
java.io.FileNotFoundException: /home/stefan/.m2/repository/org/bytedeco/javacv/1.3/javacpp-1.3.jar (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method) ~[na:1.8.0_121]
    at java.util.zip.ZipFile.<init>(ZipFile.java:219) ~[na:1.8.0_121]
..
2017-02-12 19:39:44.590  WARN 10137 --- [ost-startStop-1] o.a.tomcat.util.scan.StandardJarScanner  : Failed to scan [file:/home/stefan/.m2/repository/org/bytedeco/javacv/1.3/opencv-3.1.0-1.3.jar] from classloader hierarchy
..
java.io.FileNotFoundException: /home/stefan/.m2/repository/org/bytedeco/javacv/1.3/opencv-3.1.0-1.3.jar (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method) ~[na:1.8.0_121]
    at java.util.zip.ZipFile.<init>(ZipFile.java:219) ~[na:1.8.0_121]
..
2017-02-12 19:39:44.591  WARN 10137 --- [ost-startStop-1] o.a.tomcat.util.scan.StandardJarScanner  : Failed to scan [file:/home/stefan/.m2/repository/org/bytedeco/javacv/1.3/ffmpeg-3.2.1-1.3.jar] from classloader hierarchy
..
java.io.FileNotFoundException: /home/stefan/.m2/repository/org/bytedeco/javacv/1.3/ffmpeg-3.2.1-1.3.jar (No such file or directory)
    at java.util.zip.ZipFile.open(Native Method) ~[na:1.8.0_121]
    at java.util.zip.ZipFile.<init>(ZipFile.java:219) ~[na:1.8.0_121]
..

最后一个例外是ClassNotFoundException,它肯定会与上述错误相关联:

java.lang.ClassNotFoundException: org.apache.jasper.servlet.JspServlet
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1285) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1119) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:520) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:501) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1050) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989) ~[tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4913) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5223) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1419) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1409) [tomcat-embed-core-8.5.11.jar:8.5.11]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [na:1.8.0_121]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_121]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_121]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_121]

似乎缺少所有依赖项。问题是我必须做些什么才能解决这个问题。 As suggested我还将tomcat-embed-core依赖项放在Spring应用程序的pom.xml中,但这不起作用。

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>8.5.11</version>
</dependency>

感谢任何帮助!

1 个答案:

答案 0 :(得分:1)

对于java.io.FileNotFoundException,可以在https://stackoverflow.com/a/37857690

中找到解决方案

ClassNotFoundException可以通过添加:

来修复
<dependency>
  <groupId>org.apache.tomcat.embed</groupId>
  <artifactId>tomcat-embed-jasper</artifactId>
  <version>8.5.11</version>
</dependency>