What the classpath means how searches work
suggest changeThe 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:
- Map the class name to a relative classfile pathname
RP
. The mapping for class names to class filenames is described elsewhere. - For each entry
E
in the classpath:
- If the entry is a filesystem directory:
- 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
- If the entry is a JAR or ZIP 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.)