JSP and Beyond

a pragmatic primer on building web-based solutions with Java technologies

WARs, JARs and Classpath

What are WAR Files?
Some may consider developing software under pressure and tight deadlines a war, but this is not what was intended by the strange name. A WAR or Web Archive file contains an entire web application in a single, compressed file. To create a WAR file it is possible to ZIP an entire web application into a package with a WAR file extension. This makes deploying an application contained within a WAR file very easy compared to dealing with a less manageable directory structure of files.

For instance, to deploy a WAR file in Tomcat simply place the WAR file in a /webapps directory and restart the application server (note that in the newer version of Tomcat it is possible to use a web-based administrative console to deploy WAR files by way of file upload). This WAR will contain all JSPs and supporting materials like JARs, images and JavaScript.

What are JAR Files?
A JAR file is a ZIP file that contains Java class files. The JAR file offers a convenient way to group together a series of classes for distribution with an application. It is pretty much the same concept as the WAR file and helps to simplify management of your application and the deployment process. A common example of a JAR file would be one that a company distributes that contains an API specific to their product. This makes it easier to consume their classes, method and attributes.

Understanding Classpath
Even seasoned developers wrestle with the beast called Classpath. Although this guide is not meant to teach the basics of Java development, I think that it is nonetheless important to take a moment to speak about the Classpath environment variable.

The Classpath information which is held in an environment variable, lets a compiler or JVM know where it can find JAR and class files that you want to reference in your code. A good example might be including the mail.jar file in an application, an example that will be discussed later. Classpath provides a pointer to JAR and class files that you will want to use in a project. As you deploy your applications into different environments, it is essential that the Classpath is properly set on the target system.

To check what references are currently being made in your Classpath, you can type “echo %classpath%” in Windows or “echo $classpath” on Linux / Unix systems.