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 linux users, and to sum up and add to what others have said here, you should know the following:
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).
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.
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.
Tomcat does not use CLASSPATH. Read what to do about that here: https://tomcat.apache.org/tomcat-8.0-doc/class-loader-howto.html
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:
-cp
options overrides CLASSPATH
environment variable.-cp
and CLASSPATH
envorinment variable.Reference: How Classpath works in Java.
Paths under linux are separated by colons (:
), not semi-colons (;
), as theatrus correctly used it in his example. I believe Java respects this convention.
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.
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"
I don't like setting CLASSPATH. CLASSPATH is a global variable and as such it is evil:
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.