交互式java调用很好,但脚本中的相同失败

时间:2016-09-06 21:39:38

标签: java bash

我的脚本没有运行,但交互式地使用相同的命令似乎有效。首先,我尝试使用*通配符,它​​失败了“无法找到或加载”可能是因为我希望通配符获得Java解释,而不是Linux解释。所以我引用通配符然后它的工作原理:( Java获取通配符并解释它我相信意思是“所有JAR文件”)。接下来我从脚本中尝试相同的操作,但它失败了。

root@server0:~/serverDeployment/application# java -enableassertions 
-classpath export/ohana1/* info.zqxj.trader1.CommandProcessor help
Error: Could not find or load main class export.ohana1.commons-collections-3.2.1.jar
root@server0:~/serverDeployment/application# java -enableassertions 
-classpath export/ohana1/"*" info.zqxj.trader1.CommandProcessor help
AssertionsEnabled
echoing arguments at the runtime local date 2016 09 06 local time 17:15:01.4...
help 
END of echoed arguments.
CommandProcessor: Will process the >help< argument.
CommandProcessor: END of processing the >help< argument.
root@server0:~/serverDeployment/application# cat runner.sh
#!/bin/bash
CLASSPATH='-classpath export/ohana1/"*"'
echo "$CLASSPATH"
java -enableassertions "$CLASSPATH" info.zqxj.trader1.CommandProcessor help
exit 0

root@server0:~/serverDeployment/application# ./runner.sh
-classpath export/ohana1/"*"
Unrecognized option: -classpath export/ohana1/"*"
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.

该选项为-classpath但由于某种原因,整个-classpath export/ohana1/"*"被视为一种选择。为什么会这样?

编辑:Costi Ciudatu指出我双引用了字符串-classpath,这是真的,它在java调用的行上被双引号所以我在下面的示例中停止了这样做。但是,它仍然失败。

root@server0:~/serverDeployment/application# cat runner.sh
#!/bin/bash
CLASSPATH='-classpath export/ohana1/"*"'
echo $CLASSPATH
java -enableassertions $CLASSPATH info.zqxj.trader1.CommandProcessor help
root@server0:~/serverDeployment/application# ./runner.sh
-classpath export/ohana1/"*"
Error: Could not find or load main class info.zqxj.trader1.CommandProcessor

BTW:echo命令似乎表明双引号没有坏处,两次我都得到单引号的内容。所以echo "$VAR"似乎和echo $VAR一样,所以也许在java调用中做同样的事情也没有坏处。

1 个答案:

答案 0 :(得分:1)

这是因为您将选项和值都放在双引号中。尝试类似:

#!/bin/bash
CLASSPATH='export/ohana1/*'
java -enableassertions -cp "$CLASSPATH" info.zqxj.trader1.CommandProcessor help