tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Classpath issues with Embedded Tomcat 7
Date Thu, 19 Apr 2012 18:22:53 GMT
Hash: SHA1


On 4/19/12 1:46 PM, Adam Gordon wrote:

If this is an update, why not reply to your original thread? It's
difficult to keep everything in context when you start a new thread.

> By adding all my jars in WEB-INF/lib to my launch command
> classpath, I can get the embedded server to run, but this does not
> seem correct as then what would be the point of WEB-INF/lib?

Your instincts are correct: you shouldn't have to add
WEB-INF/lib/*.jar to your CLASSPATH. Something else must be incorrect
with your environment.

> I've been unable to find any decent documentation on how to use
> Embedded Tomcat so I've been winging it.

The Tomcat source (specifically the test cases) is rife with examples
of use of Tomcat embedded: any test case that needs a running instance
uses the Tomcat class. Look through the build script (specifically,
how tests are run) to see what libraries are added to the bootstrap
CLASSPATH when launching them.

> The issue I'm having is that from what I've gleaned from the web
> all I *should* need in my classpath for my main method is my main
> class, tomcat-embed-core.jar, tomcat-embed-logging.juli.jar,
> tomcat-embed-jasper.jar, and ecj.jar but I'm finding that when
> trying to launch my main from the command line I keep having to
> append more and more jars from WEB-INF/lib to my classpath to get 
> it to continue to try and start up because it keeps spitting out 
> NoClassDefFoundErrors.

Can you give a specific example?

I was under the impression that it would use
> WEB-INF/lib for the web app's  classpath but this is not what's
> happening. The syntax used below was used from 
see the section for running a web application.
> Here's my main:
> public static void main(String[] args) throws LifecycleException,
> InterruptedException, ServletException {
> Tomcat tomcat = new Tomcat(); tomcat.setPort(9090);
> File docBase = new File("/usr/local/foo/foo-ui"); 
> tomcat.addWebapp(null, "", docBase.getAbsolutePath());
> tomcat.start(); tomcat.getServer().await(); }
> and here's my launch command:
> user@host: /usr/local/foo/foo-ui $ java -cp 
> WEB-INF/classes:WEB-INF/lib/tomcat-embed-core-7.0.27.jar:WEB-INF/lib/tomcat-juli-7.0.27.jar:WEB-INF/lib/tomcat-embed-jasper-7.0.27.jar:WEB-INF/lib/ecj-3.7.1.jar
> Anyone have any ideas what I'm doing wrong?  Thanks.

Well, first, there's no reason to have all the Tomcat JAR files in the
webapp's WEB-INF/lib directory. Put them somewhere else. You might be
getting NCDFE because (for example) Tomcat.class != Tomcat.class when
the two are loaded by two different ClassLoaders. Take everything out
of your webapp's WEB-INF/lib directory that it doesn't use directly or
indirectly (i.e. all the Tomcat-specific stuff).

So, let's see what the javadoc for has to say:

> * Requirements: * * - all tomcat classes and possibly servlets are
> in the classpath. * ( for example all is in one big jar, or in
> eclipse CP, or in any other * combination )

Once you've moved the Tomcat .jar files, this should be true.

> * - we need one temporary directory for work files

I don't see that you've set that up: you need to call Tomcat.setBaseDir().

> * - no config file is required. This class provides methods to *
> use if you have a webapp with a web.xml file, but it is * optional
> - you can use your own servlets.

Do you have your own web.xml file? Probably. If you follow the code of
the method you are calling (not sure why you are calling the
undocumented method instead of the documented one), you'll see that
ultimately, your webapp should be deployed with no default web.xml
(the server-wide one) and the ability to load your webapp's
WEB-INF/web.xml file.

> * There are a variety of 'add' methods to configure servlets and
> webapps. These * methods, by default, create a simple in-memory
> security realm and apply it. * If you need more complex security
> processing, you can define a subclass of * this class. * * This
> class provides a set of convenience methods for configuring webapp 
> * contexts, all overloads of the method <code>addWebapp</code>.
> These methods * create a webapp context, configure it, and then add
> it to a {@link Host}. * They do not use a global default web.xml;
> rather, they add a lifecycle * listener that adds the standard
> DefaultServlet, JSP processing, and welcome * files.

What happens if you move the Tomcat classes outside of your webapp's
WEB-INF directory, adjust your CLASSPATH accordingly, and re-launch?
Be specific.

- -chris
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools -
Comment: Using GnuPG with Mozilla -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message