Is java -classpath overriding CLASSPATH or adding to it?

If i have

CLASSPATH=/blah;/foo 

Then run

 java -cp bar.jar com.yourcompany.SomeMain 

Is classpath now

 bar.jar 

Or that

 /blah;/foo;bar.jar 

Basically switching command line or expanding existing CLASSPATH ?

+6
source share
3 answers

It seems that 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 Tests folder, however, when I use the -cp , it overrides it and changes the class path . , the current directory, so my class file can no longer be found.

+4
source

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

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

+2
source

The -cp command line switch overrides. In fact, some other things override the CLASSPATH environment variable, such as the -jar switch or IDE-specific class path parameters.

In any case, if you want to quickly look at which class path is referenced when you run your code, you can consider displaying this line in your class.

 System.getProperty("java.class.path"); 
+2
source

All Articles