views:

397

answers:

4

Will the use of -classpath option with java, add to or replace the contents of the CLASSPATH env variable?

A: 

Yes. Quoted from the java(1) man page:

   -classpath classpath
   -cp classpath
          Specifies a list of directories, JAR archives, and ZIP archives to search  for  class  files.   Class
          path  entries  are separated by colons (:). Specifying -classpath or -cp overrides any setting of the
          CLASSPATH environment variable.

          If -classpath and -cp are not used and CLASSPATH is not set, the user class path consists of the cur-
          rent directory (.).
Robert Munteanu
The environment variable is *not* updated at all, according to this. I don't see how the answer is "yes".
S.Lott
@S.Lott - From whose point of view? From the *program's* point of view the CLASSPATH value (if it exists) is replaced. From the *shell's* point of view the CLASSPATH value isn't changed. I think the original question was just badly worded.
Nate
+3  A: 

Either one of the options is used, not both.

Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.

...

The -classpath option is preferred because you can set it individually for each application without affecting other applications and without other applications modifying its value.

...

Setting the CLASSPATH variable or using the -classpath command-line option overrides that default, so if you want to include the current directory in the search path, you must include "." in the new settings.

The MYYN
+1  A: 
sateesh
+2  A: 

Using the classpath variable it overrides the CLASSPATH of Environment variable but only for that session. If you restart the application you need to again set the classpath variable.

giri
Perfect. Thanks girienie for a concise answer. You must be a Java expert :)
Zacky112
@Zack112:Very important question.. Thanks :)
giri