Documentation

The Java™ Tutorials
Hide TOC
Adding Classes to the JAR File's Classpath
Trail: Deployment
Lesson: Packaging Programs in JAR Files
Section: Working with Manifest Files: The Basics

Adding Classes to the JAR File's Classpath

Sometimes you may need to reference classes or resources residing outside your application's JAR file. You can use the Class-Path manifest attribute for this purpose.

In the Class-Path manifest attribute of your application's JAR file, you can specify one or more relative URLs referring to JAR files or directories containing the classes and resources that your application needs. These URLs are treated as a relative reference and not an absolute reference (see the URI.isAbsolute() method). That is, these relative URLs don't contain a scheme component to the code base from which the application's JAR file containing the Class-Path attribute was loaded.

Separate each value in the Class-Path attribute with either the space character, the tab character, the newline character, the carriage-return character, or the form-feed character.

Relative URLs in the Class-Path attribute value that don't end with a slash (/) are assumed to refer to JAR files.

The following is an example of the Class-Path manifest attribute:

Class-Path: servlet.jar infobus.jar acme/beans.jar images/

By using the Class-Path attribute in your application's JAR file manifest, you can avoid having to specify a long -classpath flag when launching java to run your application.


Note: The Class-Path manifest attribute value doesn't refer to JAR files or directories within the application JAR file. To load classes and resources from JAR files within a JAR file, you must write custom code. For example, if MyJar.jar contains another JAR file called MyNested.jar, you cannot use the Class-Path attribute in MyJar.jar's manifest to load classes in MyNested.jar.

An Example

Let's consider the case where from within a class in MyJar.jar we want to load some classes and resources that reside in the following locations:

Our goal is to build the MyJar.jar JAR file with a relevant Class-Path attribute such that it can access classes and resources from these locations. On the file system, all of these JAR files and directories reside in the same root directory. Let's consider that root directory to be MyApp.

First, from within the MyApp directory, we create a text file named Manifest.txt with the following contents:

Class-Path: MyUtils.jar MyLibs/Lib.jar MyResources/ 

Warning: This text file must end with a new line or carriage return. The last line won't be parsed properly if it doesn't not end with a new line or carriage return.

We then create a JAR file named MyJar.jar with the following command:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

This creates the MyJar.jar JAR file with a manifest with the following contents:

Manifest-Version: 1.0
Class-Path: MyUtils.jar MyLibs/Lib.jar MyResources/
Created-By: 1.8.0_422 (Oracle Corporation)

Consequently, when the Java application is launched from within the MyApp directory, the classes in MyJar.jar, in addition to being able to access the classes and resources that are part of MyJar.jar, can also access classes and resources that are contained in the MyUtils.jar and MyLibs/Lib.jar JAR files and the MyResources/ directory.

See Class-Path Attribute in JAR File Specification for more information.


Previous page: Setting an Application's Entry Point
Next page: Setting Package Version Information