What the classpath means how searches work

suggest change

The purpose of the classpath is to tell a JVM where to find classes and other resources. The meaning of the classpath and the search process are intertwined.

The classpath is a form of search path which specifies a sequence of locations to look for resources. In a standard classpath, these places are either, a directory in the host file system, a JAR file or a ZIP file. In each cases, the location is the root of a namespace that will be searched.

The standard procedure for searching for a class on the classpath is as follows:

  1. Map the class name to a relative classfile pathname RP. The mapping for class names to class filenames is described elsewhere.
  2. For each entry E in the classpath:
- Resolve `RP` relative to `E` to give an absolute pathname `AP`.
- Test if `AP` is a path for an existing file.
- If yes, load the class from that file
- Lookup `RP` in the JAR / ZIP file index.
- If the corresponding JAR / ZIP file entry exists, load the class from that entry.

The procedure for searching for a resource on the classpath depends on whether the resource path is absolute or relative. For an absolute resource path, the procedure is as above. For a relative resource path resolved using Class.getResource or Class.getResourceAsStream, the path for the classes package is prepended prior to searching.

(Note these are the procedures implemented by the standard Java classloaders. A custom classloader might perform the search differently.)

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


The Classpath:
* What the classpath means how searches work

Table Of Contents
8 Arrays
10 Maps
11 Strings
25 JAXB
29 Enums
32 Audio
41 Scanner
63 Logging
75 Lists
78 Sets
84 The Classpath
89 JAX-WS
96 XJC
98 Process
106 Modules
114 Applets
122 JNDI
139 JavaBean
141 Literals
144 Packages
150 JMX
153 JShell
159 Sockets
167 Enum Map
175 Hashtable
177 SortedMap