Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does java --classpath override CLASSPATH or append to it? [duplicate]

Tags:

java

classpath

If I have

CLASSPATH=/blah;/foo

Then run

java -cp bar.jar com.yourcompany.SomeMain

Is the classpath now

bar.jar

OR is it

/blah;/foo;bar.jar

Basically does the command line switch override or augment the existing CLASSPATH?

like image 907
Doug T. Avatar asked Mar 25 '13 01:03

Doug T.


3 Answers

Looks like the -cp option overrides the CLASSPATH environment variable.

$ export CLASSPATH=Tests
$ java Printf
Team Name                      No. of Wins          No. of Losses        
Bobcats                        0                    0                    
Tigers                         1                    1                    
Lions                          2                    2                    
Cheetahs                       3                    3                    
Jackals                        4                    4                    
Leopards                       5                    5                    
Snow Leopards                  6                    6                    
Cougars                        7                    7                    
Mountain Lions                 8                    8                    
Bobcats                        9                    9                    
$ java -cp . Printf
Error: Could not find or load main class Printf

The CLASSPATH environment variable is still set to the folder Tests, however, when I use the -cp option it overrides it, and changes the classpath to ., the current directory, so my class file can no longer be found.

like image 162
ktm5124 Avatar answered Sep 18 '22 11:09

ktm5124


yes!, the cp or classpath option overrides the system variable. for more information

http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/java.html

like image 41
solaechea Avatar answered Sep 18 '22 11:09

solaechea


The -cp command line switch overrides. In fact, a few other things override the CLASSPATH environment variable like the -jar switch, or IDE-specific classpath settings.

In any case, if you want to have a quick look at which classpath gets referenced when running your code, you can consider displaying this line in your class.

System.getProperty("java.class.path");
like image 31
Jops Avatar answered Sep 21 '22 11:09

Jops