Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Whitespaces in CLASSPATH

I am working on a Windows PC and have cygwin on it! I have organized all my jars under a directory within a few directories! I am writing a bash script to set the CLASSPATH by iterating through the directory that is passed as a parameter as follows:

for JAR_FILE in `ls *.jar`
do
    CLASSPATH="$DIRECTORY_TO_LOOK_FOR_JARS"/$JAR_FILE:$CLASSPATH
done

Whenever there are spaces in the directory that is passed like /cygdrive/c/Documents and Settings/user/My Jars and I run java -cp $CLASSPATH somepackage.someclass, it throws an error stating that the class and is not found, because the CLASSPATH variable is getting split after /cygdrive/c/Documents.

Can someone help me to solve this issue?

like image 968
Shyam Avatar asked Jun 09 '10 07:06

Shyam


1 Answers

See this article.

You could enclose either the full classpath in double qoutes

java -classpath "C:/Documents and Settings/user/project/lib/axis.jar; C:/Documents and Settings/user/project/lib/axis-ant.jar;" TestClient

or each jar in your classpath

java -classpath "C:/Documents and Settings/user/project/lib/axis.jar"; "C:/Documents and Settings/user/project/lib/axis-ant.jar;" TestClient

In your case something like that:

for JAR_FILE in `ls *.jar`
do
    CLASSPATH="$DIRECTORY_TO_LOOK_FOR_JARS/$JAR_FILE":$CLASSPATH
done

Hard to see, but I moved the closing double qoute.

like image 172
stacker Avatar answered Oct 23 '22 06:10

stacker