13
export CLASSPATH=.;../somejar.jar;../mysql-connector-java-5.1.6-bin.jar
java -Xmx500m folder.subfolder../dit1/some.xml
cd ..

is the above statement for setting the classpath to already existing classpath in linux is correct or not

John Kugelman
  • 349,597
  • 67
  • 533
  • 578

5 Answers5

27

I don't like setting CLASSPATH. CLASSPATH is a global variable and as such it is evil:

  • If you modify it in one script, suddenly some java programs will stop working.
  • If you put there the libraries for all the things which you run, and it gets cluttered.
  • You get conflicts if two different applications use different versions of the same library.
  • There is no performance gain as libraries in the CLASSPATH are not shared - just their name is shared.
  • If you put the dot (.) or any other relative path in the CLASSPATH that means a different thing in each place - that will cause confusion, for sure.

Therefore the preferred way is to set the classpath per each run of the jvm, for example:

java -Xmx500m -cp ".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar"    "folder.subfolder../dit1/some.xml

If it gets long the standard procedure is to wrap it in a bash or batch script to save typing.

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
flybywire
  • 261,858
  • 191
  • 397
  • 503
  • can u give me the entire cmd. the cmd u have specified is not working –  Feb 24 '09 at 08:37
  • the cmd that i have used export classpath= java -Xmx500m -cp ".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar" "folder.subfolder../dit1/some.xml –  Feb 24 '09 at 08:38
  • Throw the export part away! Just: java -Xmx500m -cp ".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar" folder.subfolder ../dit1/some.xml – boutta Feb 24 '09 at 09:54
  • 3
    "global variables" are not evil. They are there to be inherited. You can override the setting in your command line "local" version with no problems. More to the point this "global variable" as you term it is important for running java apps you might have installed via synaptic/aptitude/apt-get. Want localised versions? Use them. – RichieHH Oct 21 '13 at 12:24
19

It's always advised to never destructively destroy an existing classpath unless you have a good reason.

The following line preserves the existing classpath and adds onto it.

export CLASSPATH="$CLASSPATH:foo.jar:../bar.jar"
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Yann Ramin
  • 32,895
  • 3
  • 59
  • 82
  • when i used the command echo $CLASSPATH nothing is displayed except empty line –  Feb 24 '09 at 08:13
  • That would imply that CLASSPATH has never been set, or has been explicitly unset. – Ian McLaird Feb 24 '09 at 21:18
  • 2
    Better advice would be not to use the CLASSPATH environment variable at all. Use "-cp" instead, and write wrapper scripts or shell aliases to save typing. – Stephen C Dec 01 '11 at 10:39
15

Important difference between setting Classpath in Windows and Linux is path separator which is ";" (semi-colon) in Windows and ":" (colon) in Linux. Also %PATH% is used to represent value of existing path variable in Windows while ${PATH} is used for same purpose in Linux (in the bash shell). Here is the way to setup classpath in Linux:

export CLASSPATH=${CLASSPATH}:/new/path

but as such Classpath is very tricky and you may wonder why your program is not working even after setting correct Classpath. Things to note:

  1. -cp options overrides CLASSPATH environment variable.
  2. Classpath defined in Manifest file overrides both -cp and CLASSPATH envorinment variable.

Reference: How Classpath works in Java.

Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71
Chui
  • 151
  • 1
  • 2
6

Paths under linux are separated by colons (:), not semi-colons (;), as theatrus correctly used it in his example. I believe Java respects this convention.

Edit

Alternatively to what andy suggested, you may use the following form (which sets CLASSPATH for the duration of the command):

CLASSPATH=".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar" java -Xmx500m ...

whichever is more convenient to you.

Community
  • 1
  • 1
David Hanak
  • 10,754
  • 3
  • 31
  • 39
2

For linux users, and to sum up and add to what others have said here, you should know the following:

  1. Global variables are not evil. $CLASSPATH is specifically what Java uses to look through multiple directories to find all the different classes it needs for your script (unless you explicitly tell it otherwise with the -cp override).

  2. The colon (":") character separates the different directories. There is only one $CLASSPATH and it has all the directories in it. So, when you run "export CLASSPATH=...." you want to include the current value "$CLASSPATH" in order to append to it. For example:

    export CLASSPATH=.
    export CLASSPATH=$CLASSPATH:/usr/share/java/mysql-connector-java-5.1.12.jar
    

    In the first line above, you start CLASSPATH out with just a simple 'dot' which is the path to your current working directory. With that, whenever you run java it will look in the current working directory (the one you're in) for classes. In the second line above, $CLASSPATH grabs the value that you previously entered (.) and appends the path to a mysql dirver. Now, java will look for the driver AND for your classes.

  3. echo $CLASSPATH
    

    is super handy, and what it returns should read like a colon-separated list of all the directories you want java looking in for what it needs to run your script.

  4. Tomcat does not use CLASSPATH. Read what to do about that here: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html

Adam Winter
  • 1,680
  • 1
  • 12
  • 26