1

I'm trying to load a "war" file to the embedded tomcat server of Spring boot. For doing that I found this answer: Spring Boot: How to add another WAR files to the embedded tomcat?

But after I do that, I found the same error already asked on this question: Deploying existing war with embedded Tomcat but without a satisfactory answer.

NOTE: I know it can be seen as a duplicate question (I put the link), but not having at the time enough reputation for add a comment, or the ability to contact the author in private, i have seen the need to repeat the question hoping for some different answer.

In particular I'm try to load the Sesame war files allocated on "resources/war/" folder of the spring boot project.

My SpringBootapplication:

@SpringBootApplication
@ComponentScan("com.github.p4535992.mvc")
public class JspDemoApplication extends SpringBootServletInitializer implements WebApplicationInitializer{

public static void main(String[] args) {
    SpringApplication.run(JspDemoApplication.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(JspDemoApplication.class);
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    SpringApplication.run(JspDemoApplication.class);
}

/*
 * https://stackoverflow.com/questions/31374726/spring-boot-how-to-add-another-war-files-to-the-embedded-tomcat
 * @return the {@link TomcatEmbeddedServletContainerFactory}.
 */
@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
            try {
                //tomcat.addUser("tomcat", "tomcat");
                //tomcat.addRole("tomcat", "manager-gui");
                String webappDirLocation = System.getProperty("user.dir")+"/src/main/resources/war/";
                //When the spring boot application is made of fat jar(=executable jar),
                // the above code is not enough             
                Context context1 = tomcat.addWebapp("/sesame", new
                        File(webappDirLocation+"openrdf-sesame.war").getAbsolutePath());
                Context context2 =  tomcat.addWebapp("/workbench",new
                        File(webappDirLocation+"openrdf-workbench.war").getAbsolutePath());
                WebappLoader loader = new WebappLoader(Thread.currentThread().getContextClassLoader());
                context1.setLoader(loader);
                context2.setLoader(loader);

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

    };
}
}

My Old Exception: old_exception

UPDATE:

NOTE: I work on Windows just to clarify.

  1. Add to the application.properties file the argument : server.tomcat.basedir=${java.io.tmpdir}.
  2. Set the JAVA_OPTIONS: -Djava.io.tmpdir=C:\Users\Utente\Desktop\path\springMVC12\Temp.
  3. Manually create the directory "sesame" and "workbench" on the "Temp" directory (for more details or other way to do that see comment of OrangeDog)
  4. Create a class AppConfig.java with @Configuration Spring Annotation, cut and paste the method "EmbeddedServletContainerFactory" from JspDemoApplication.java to AppConfig.java.

Now no exception is thrown, but when i try to call the webapp on the address http://localhost:8081/openrdf-sesame , http://localhost:8081/sesame , i get the error 404 resource not found.

e.g. "status":404,"error":"Not Found","message":"No message available"

So now it's seems the war file is loaded, but i must miss something on the set of the http url of the web app.

UPDATE 2 The new Application.java of Spring Boot:

@SpringBootApplication
@ComponentScan("com.github.p4535992.mvc")

public class MainApp extends SpringBootServletInitializer implements WebApplicationInitializer{

public static void main(String[] args) {
    SpringApplication.run(MainApp.class, args);
}

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(MainApp.class);
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException {
    //com.github.p4535992.mvc.component.ScheduledTasks --> Work
    SpringApplication.run(MainApp.class);
}

@Bean
public EmbeddedServletContainerFactory servletContainerFactory() {
    return new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(Tomcat tomcat) {
            try {
                String webappDirLocation = "src/main/resources/war/";
                tomcat.addWebapp("/sesame", new
                        File(webappDirLocation+"openrdf-sesame.war").getAbsolutePath());
                tomcat.addWebapp("/workbench",new
                        File(webappDirLocation+"openrdf-workbench.war").getAbsolutePath());
            } catch (ServletException ex) {
                throw new IllegalStateException("Failed to add webapp", ex);
            }
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

    };
}
}

NOw i have a new exception:

2016-04-09_10:25:01.998 [Tomcat-startStop-1] ERROR org.apache.catalina.core.ContainerBase - A child container failed during start
java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
at java.util.concurrent.FutureTask.report(FutureTask.java:122)
at java.util.concurrent.FutureTask.get(FutureTask.java:192)
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:916)
at org.apache.catalina.core.StandardHost.startInternal(StandardHost.java:871)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:153)
... 6 common frames omitted
Caused by: java.lang.IllegalStateException: java.lang.NullPointerException
at org.springframework.boot.context.embedded.tomcat.TomcatResources$Tomcat7Resources.addJar(TomcatResources.java:125)
at org.springframework.boot.context.embedded.tomcat.TomcatResources.addClasspathResources(TomcatResources.java:63)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory$StoreMergedWebXmlListener.onStart(TomcatEmbeddedServletContainerFactory.java:746)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory$StoreMergedWebXmlListener.lifecycleEvent(TomcatEmbeddedServletContainerFactory.java:737)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
... 6 common frames omitted
Caused by: java.lang.NullPointerException: null
at org.springframework.boot.context.embedded.tomcat.TomcatResources$Tomcat7Resources.addJar(TomcatResources.java:122)
... 13 common frames omitted
2016-04-09_10:06:32.378 [localhost-startStop-1] ERROR o.a.c.c.ContainerBase.[Tomcat].[localhost].[/sesame] - Servlet [jsp] in web application [/sesame] threw load() exception
java.lang.ClassNotFoundException: org.apache.jasper.servlet.JspServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1038)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4997)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5289)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
2016-04-09_10:06:32.568 [localhost-startStop-1] ERROR o.a.c.c.C.[Tomcat].[localhost].[/workbench] - Servlet [jsp] in web application [/workbench] threw load() exception
java.lang.ClassNotFoundException: org.apache.jasper.servlet.JspServlet
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1308)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1142)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:518)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:499)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:118)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1102)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1038)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4997)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5289)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

I try to resolve the issue adding the maven dependency but it doesn't work:

 <!-- Support for add war to the project -->
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jasper</artifactId>
        <version>9.0.0.M4</version>
    </dependency>
    <!-- https://stackoverflow.com/questions/4501829/unable-to-load-class-for-jsp -->
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>javax.servlet.jsp-api</artifactId>
        <version>2.3.2-b02</version>
        <scope>provided</scope>
    </dependency>

Any help is more then welcome.

Community
  • 1
  • 1
4535992
  • 418
  • 9
  • 29
  • How is this not a duplicate of the question you have linked? – OrangeDog Apr 02 '16 at 13:22
  • 1
    Possible duplicate of [Unable to create the directory error](http://stackoverflow.com/questions/28271111/unable-to-create-the-directory-error) – OrangeDog Apr 02 '16 at 13:29
  • OK, that can be the solution but how can accomplish that on a windows machine? – 4535992 Apr 03 '16 at 10:24
  • I'm having a similar issue ([see here](http://stackoverflow.com/questions/42052345/how-can-i-deploy-a-war-file-to-the-built-in-tomcat-server-of-a-spring-boot-appli)) but I don't think it's a permission issue. Did you find a solution for your problem? – Stefan Falk Feb 08 '17 at 19:10
  • sorry man no after all this time , i haven't find a solution for this – 4535992 Feb 14 '17 at 08:31

1 Answers1

1

i found the same error already asked on this question: Deploying existing war with embedded Tomcat but without a satisfactory answer.

Because such configuration doesn't make sense for Spring Boot. You have two options with Spring Boot:

  1. WAR, deployed on shared and managed servlet container
  2. fat JAR, where your single application will be running on single embedded servlet container

Read this section of Spring Boot docs for more info.

Also notice that question you are referring to is not Spring/Spring Boot related.

Community
  • 1
  • 1
luboskrnac
  • 23,973
  • 10
  • 81
  • 92
  • Sorry lubo I have to be really stupid, I read the documentation but I can not find the solution that interests me, would you be so kind as to give me an example? – 4535992 Apr 09 '16 at 08:40