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
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.
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"
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:
-cpoptions overridesCLASSPATHenvironment variable.- Classpath defined in Manifest file overrides both
-cpandCLASSPATHenvorinment 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.
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.
来源:https://stackoverflow.com/questions/580860/adding-classpath-in-linux