在 Linux 脚本中设置 classPath
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9664576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting classPath inside Linux Script
提问by Pawan
I wrote a simple Linux Script this way
我用这种方式写了一个简单的 Linux 脚本
export JAVA_HOME=/usr/local/jdk1.6.0_20
export PATH=/usr/local/jdk1.6.0_20/bin
LIB_DIR=/home/praveen/lib
export CLASSPATH=.:$LIB_DIR/commons-logging-1.0.4.jar:$LIB_DIR/log4j-1.2.8.jar
java -cp $CLASSPATH com.test.Sample
===============================================================================
================================================== ==============================
The above script was working fine .
上面的脚本运行良好。
But when i tried for the first time with this , it was giving an error
但是当我第一次尝试这个时,它给出了一个错误
export JAVA_HOME=/usr/local/jdk1.6.0_20
export PATH=/usr/local/jdk1.6.0_20/bin
export CLASSPATH = /home/praveen/lib/commons-logging-1.0.4.jar: /home/praveen/lib/log4j-1.2.8.jar
java -cp $CLASSPATH com.test.Sample
As you can observe the only difference between these two scripts is that , in the below script on to the class path , i am not including the current directory path ( .)
您可以观察到这两个脚本之间的唯一区别是,在下面的类路径脚本中,我不包括当前目录路径 ( . )
Please let me know , why this will not work this way ??
请让我知道,为什么这不起作用?
采纳答案by Leonard Garvey
You need to use:
您需要使用:
export CLASSPATH=/home/praveen/lib/commons-logging-1.0.4.jar:/home/praveen/lib/log4j-1.2.8.jar
rather than:
而不是:
export CLASSPATH = /home/praveen/lib/commons-logging-1.0.4.jar: /home/praveen/lib/log4j-1.2.8.jar
You can't put spaces in between the variable name and the equals sign in shell scripting.
在 shell 脚本中,不能在变量名和等号之间放置空格。
回答by Leonard Garvey
Try this one..:
试试这个..:
#!/bin/bash
export JAVA_HOME=...
cp=$(find lib -name "*.jar" -exec printf :{} ';')
if [[ -n "$CLASSPATH" ]]; then
cp="$cp;CLASSPATH"
fi
"$JAVA_HOME/bin/java" -classpath "$cp" ...
回答by sirgeorge
You should not put any spaces in when you are setting variable, not even spaces around '=', it should be:
设置变量时不应该输入任何空格,甚至不应该在 '=' 周围放置空格,它应该是:
export CLASSPATH=/home/praveen/lib/commons-logging-1.0.4.jar:/home/praveen/lib/log4j-1.2.8.jar
If you have spaces in one of the elements use single or double quotations e.g.:
如果其中一个元素中有空格,请使用单引号或双引号,例如:
MY_VAR1=' variable with spaces'
MY_VAR2=variable_without_spaces
export MY_VAR3="${MY_VAR1}${MY_VAR2}"