Search code examples
javatomcatjarwarspark-java

How to create WAR for Spark 1.1.1 application?


I've created nice application using Spark. Now I want to deploy it to Apache Tomcat 7. I've tried to build WAR using Apache Ant:

<target name="war">
  <war destfile="ROOT.war"
       webxml="web.xml">
     <classes dir="classes"/>
  </war>
</target>

I've placed compiled code into classes folder. In web.xml I've placed officially recommended code:

<web-app>
    <filter>
        <filter-name>SparkFilter</filter-name>
        <filter-class>spark.servlet.SparkFilter</filter-class>
        <init-param>
            <param-name>applicationClass</param-name>
            <param-value>com.MyApplication</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>SparkFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
</web-app>

Then I've placed ROOT.war into webapps folder, restarted Tomcat and my application isn't deployed.

I've checked catalina.out

java.lang.IllegalStateException: ContainerBase.addChild: start: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:904)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1073)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

Looks not very informative. Then I've checked localhost logs.

Feb 28, 2016 4:02:24 AM org.apache.catalina.core.StandardContext filterStart
SEVERE: Exception starting filter SparkFilter
java.lang.ClassNotFoundException: spark.servlet.SparkFilter
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
    at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:529)
    at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:511)
    at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
    at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:258)
    at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:105)
    at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:4809)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5485)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:901)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:877)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:632)
    at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:1073)
    at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1857)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)

Hm, looks like there is no some Spark JAR in classpath. I've added /usr/share/tomcat7/lib/spark-core-1.1.1.jar

It looks like there is no spark.servlet.SparkFilter there. Do you have any ideas where I can find this jar? Or if anyone has WAR file ready to deploy, could you share it with me?

I have checked that jar files from /usr/share/tomcat7/lib/ are used by other servlets.


Solution

  • I have added an example of how you build a war with maven and sparkjava 2.3.

    Here is the repository https://github.com/kliakos/sparkjava-war-example with instructions.

    The steps are fairly simple. It should work with a fresh Tomcat installation, otherwise it may be some Tomcat related problem you are facing.