Deploy a web application to an embedded Tomcat 8

I’ve found various tutorials on creating an embedded tomcat programatically and deploy a web application to it. Unfortunately, non of the tutorials was up to date using Tomcat 8 and actually non was showing how to deploy anything else but a servlet so I decided to write a short tutorial on how to create the server and deploy either a WAR file or a folder containing a non archived web application.

Create a simple Java project in ecplipse, enable maven and add the following dependencies:


Optionally, you can enable the maven-assembly-plugin to package your application as JAR at the end and have maven include all dependencies you specified. As build goal you’ll have to use assembly:single.


Now create the class de.jofre.embeddedtc.runtime.Main (The name is arbitrary) and add write the code according to the next listing:

package de.jofre.embedded.runtime;
import java.util.logging.Logger;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.startup.Tomcat;
public class Main {
  private final static Logger LOGGER = Logger.getLogger(Main.class.getName());
  private final static String mWorkingDir = System.getProperty("");
  private static Tomcat tomcat = null;
  public static void main(String[] args) {
    tomcat = new Tomcat();
    try {
    } catch (LifecycleException e) {
      LOGGER.severe("Tomcat could not be started.");
    }"Tomcat started on " + tomcat.getHost());
    // Alternatively, you can specify a WAR file as last parameter in the following call e.g. "C:\\Users\\admin\\Desktop\\app.war"
    Context appContext = Main.getTomcat().addWebapp(Main.getTomcat().getHost(), "/app", "C:\\Users\\admin\\Desktop\\app\\");"Deployed " + appContext.getBaseName() + " as " + appContext.getBaseName());

The last question is how the directory in C:\\Users\\admin\\Desktop\\app\\ respectively the C:\\Users\\admin\\Desktop\\app.war looks like. Well, it contains a simple HTML file…


… and another folder called WEB-INF containing the web.xml with the following content:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app 
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    <display-name>Test App</display-name>
    <description>A test app</description>

Now if you start the java application you call http://localhost:8080/app and the content of index.html should be displayed. Hope this helps you!

6 thoughts on “Deploy a web application to an embedded Tomcat 8”

  1. Hi Andreas,
    I have worked in Tomcat7, in conf folder of Tomcat you have wrapper.conf file in that u can add your custom directory.

  2. I am trying to deploy running Spring maven application in WAR through embedded tomcat JAR.

    I am able to deploy it successfully, (able to see the systems out println from deployed program) but while accessing it in browser through context , it fails :

    error : org.apache.catalina.core.StandardWrapperValve invoke
    SEVERE: Servlet.service() for servlet [jsp] in context with path [/webapp] threw exception [java.lang.NoSuchMethodError: org.apache.tomcat.JarScanner.scan(Ljavax/servlet/ServletContext;Ljava/lang/ClassLoader;Lorg/apache/tomcat/JarScannerCallback;Ljava/util/Set;)V] with root cause
    java.lang.NoSuchMethodError: org.apache.tomcat.JarScanner.scan(Ljavax/servlet/ServletContext;Ljava/lang/ClassLoader;Lorg/apache/tomcat/JarScannerCallback;Ljava/util/Set;)V

  3. Don`t you get an error during deployment (The method getTomcat() is undefined for the
    type Main…)? Is that really correct: Main.getTomcat(). or do you have a getTomcat method somewhere?

    1. Hi Deme, some days have passed since I wrote that article. I think I wrote a method getTomcat() to treat the Tomcat object as singleton. You might want to replace “Main.getTomcat()” by “tomcat” – bet it’ll work as well.

Leave a Reply

Your email address will not be published. Required fields are marked *