Java Language Different ways to specify the classpath


Example

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
    
  2. 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.

  3. 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: