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.
- Add to the application.properties file the argument : server.tomcat.basedir=${java.io.tmpdir}.
- Set the JAVA_OPTIONS: -Djava.io.tmpdir=C:\Users\Utente\Desktop\path\springMVC12\Temp.
- Manually create the directory "sesame" and "workbench" on the "Temp" directory (for more details or other way to do that see comment of OrangeDog)
- 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.