The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.
See Dev.java for updated tutorials taking advantage of the latest releases.
See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.
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.
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
.
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:
MyUtils.jar
JAR fileMyResources
directoryLib.jar
JAR file that resides in the MyLibs
child
directoryOur 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/
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.