Different ways to specify the classpath

suggest change

There are three ways to set the classpath.

  1. It can be set using the CLASSPATH environment variable :
set CLASSPATH=...         # Windows and csh
export CLASSPATH=...      # Unix ksh/bash
  1. It can be set on the command line as follows
java -classpath ...
javac -classpath ...

Note that the -classpath (or -cp) option takes precedence over the CLASSPATH environment variable.

  1. The classpath for an executable JAR file is specified using the Class-Path element in MANIFEST.MF:
Class-Path: jar1-name jar2-name directory-name/jar3-name

Note that this only applies when the JAR file is executed like this:

java -jar some.jar ...

In this mode of execution, the -classpath option and the CLASSPATH environment variable will be ignored, even if the JAR file has no Class-Path element.

If no classpath is specified, then the default classpath is the selected JAR file when using java -jar, or the current directory otherwise.

Related:

Feedback about page:

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


The Classpath:
* Different ways to specify the classpath

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