views:

4157

answers:

3

Hi,

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

+3  A: 

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

export CLASSPATH="$CLASSPATH:foo.jar:../bar.jar"
Yann Ramin
when i used the commandecho $CLASSPATHnothing is displayed except empty line
That would imply that CLASSPATH has never been set, or has been explicitly unset.
Ian McLaird
+2  A: 

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.

David Hanak
+4  A: 

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 wo different applications use different versions of the same libray.
  • 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 prefered 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.

flybywire
can u give me the entire cmd. the cmd u have specified is not working
the cmd that i have usedexport classpath= java -Xmx500m -cp ".:../somejar.jar:../mysql-connector-java-5.1.6-bin.jar" "folder.subfolder../dit1/some.xml
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