Initially, to locate the classes of our program we had to tell JVM by providing the CLASSPATH Environment Variables by building the ClassPath, it held true for command line programs as well whenever we did java and javac. But eventually it was learnt that this approach would be troublesome in the long run making things messier. So, the globalizing was then thought of being replaced by per program thing. Meaning having the libraries and classes included in each and every program.

All was well until advent of Servlets.

The Servlet API was created where a web application is a stand-alone unit – this resulted in that the CLASSPATH for each web application is the unpacked files under WEB-INF/classes plus the jar-files under WEB-INF/lib. And only that. This means the global CLASSPATH variable is ignored. This has been found to be a VERY good thing, so the concept has migrated elsewhere.

For instance a “executable jar” which is invoked with “java -jar foobar.jar” contains the complete classpath INSIDE the Jar in a special manifest file. Java Web Start which is used to start java programs from a web server explicitly lists the full classpath in the configuration file on the server.

But, to get you started. If you want to write a Java web application:

  1. Get the Eclipse Java EE version.
  2. Create a new Dynamic Web Project e.g. named foobar.
  3. Drag and drop (or copy/paste) the jar files you need in foobar/WebContent/WEB-INF/lib
  4. Create a new file named foobar/WebContent/index.jsp. In the blank file type <h1>Hello World <%= new java.util.Date() %></h1>
  5. Right click in editor for index.jsp, choose Run -> Run on Server, and choose the Basic -> J2EE preview at localhost server, and Finish.

A browser window will now open, either in a browser or inside Eclipse which will render your JSP-page.

 

So to brief it all: If you create a Dynamic Web Project in Eclipse for Java EE developers, then Eclipse will automatically add any libraries in WEB-INF/lib to the build path. The build path is roughly said just the classpath which is been used in both compiletime and runtime. In other words: just drop the 3rd party JAR’s in there, really nothing more needs to be done.This is where servlet containers expect to find an application’s jar files.

 

Hope this information was useful and please comment if anything is left out.

Leave a Reply