In command `java`, does `-cp` override `$CLASSPATH`, or prefix it?

Tim :

When running a .class program with command java, if a dependency is not found in -cp, will it be searched for in environment variable $CLASSPATH? In otherwords, does -cp override $CLASSPATH, or prefix it?

I saw an example:

java -cp "${CLASSPATH}:${SCALA_HOME}/lib/scala-library.jar:target/scala-2.10/basic_2.10-1.0.jar" foo.bar.baz.Main

What is the purpose of adding ${CLASSPATH} to -cp?

Thanks.

lainatnavi :

If you want to use CLASSPATH along with -cp you need to add it in the -cp. -cp does not imply the use of the env variable CLASSPATH. The possible scenarios are:

  1. CLASSPATH not set, no use of -cp: the default classpath is used.
  2. CLASSPATH set, no use of -cp: the directories in CLASSPATH are used.
  3. -cp is set: the directories passed via the option are used.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=301059&siteId=1