CLASSPATH中的空白


问题内容

我正在Windows
PC上工作,并且有cygwin!我将我所有的jars组织在几个目录中的一个目录下!我正在编写一个bash脚本,以CLASSPATH通过遍历作为参数传递的目录来设置,如下所示:

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

每当在目录中有空格传递/cygdrive/c/Documents and Settings/user/My Jars并且我运行时java -cp $CLASSPATH somepackage.someclass,都会抛出错误,指出and未找到该类,因为CLASSPATH变量在之后被拆分/cygdrive/c/Documents

有人可以帮我解决这个问题吗?


问题答案:

看到这篇文章

您可以将完整的类路径包含在双qoutes中

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

或类路径中的每个jar

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

在你的情况是这样的:

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

很难看到,但是我移动了结尾的双qoute。