File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Servlet programming - Jar files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet programming - Jar files" Watch "Servlet programming - Jar files" New topic

Servlet programming - Jar files

Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
I have import stmt (like import org.apache.* etc ) on top of a servlet so I believe that I need to have those files unjarred in classes directory for servlet to compile. Now at run time do I still need jar file to run the servlet. My servlet does not compile unless I unjar and it does not run unless I put jar in lib. Sound weird. I should not be needing to do unjar whiel compile.
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

You're quite right: you don't need to unjar it to compile or run, but in both cases you need to make it available on the classpath.

The trick here is that the compile time and runtime classpath (for app servers) is different. If you compile from the command line you need to add the jar (and the whole jar, not just the path to the jar) to your classpath, but at runtime it will find the jar in the WEB-INF/lib directory.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

You don't need to unjar anything to compile or run a servlet.
You just need to have the needed jar files on your classpath.

Unlike with classes, which only need the parent directory on the classpath, jar files have to be added directly to the classpath for their contents to be accessible.

export CLASSPATH=$CLASSPATH:/usr/lib/j2ee.jar

or for windows:

set CLASSPATH=%CLASSPATH%;c:\j2ee.jar

Which conatiner are you using?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Servlet programming - Jar files
It's not a secret anymore!