Why shouldn’t we place tomcat library in our appln libraries

I have a web application running in Tomcat 7.
When I place the tomcat default libraries (jsp-api.jar,servlet-api.jar etc.) in my application/WebContent/WEB-INF/lib, it throws an exception : java.lang.IllegalStateException: No org.apache.tomcat.InstanceManager set in ServletContext

When I delete these jars from my lib and and set tomcat lib in build.xml, it works fine. Can anyone explain to me why this happens?
Why can’t I use tomcat libraries in my lib folder?

Answer

You basically don’t want to add Servlet, JSP, EL or any container-related jars into your application WEB-INF/lib folder.

These libraries are to be provided by the container. If you add them explicitly, the deployed application will have a problem (i.e. two same jars into the same classpath).

EDIT: If you need these jars in order for the code to compile than you can add them to your classpath – they don’t need to reside in your application’s WEB-INF/lib.

I.e. if you’re using Eclipse you can add ‘user library’: Properties -> Java Build Path -> Libraries -> Add Library which will define all container-related libs; you can also Add External JARs and just select those from your tomcat7 directory.

EDIT 2: As BalusC pointed: “you can just reference it as target runtime, then Eclipse will do the magic. See also the related link which I commented on the question. No need to fiddle with user libraries.”

Leave a Reply

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