简体   繁体   English

Java代码无法解析从包装脚本传递的命令行参数

[英]Java code fails to parse command line arguments pass from wrapper script

I have pretty simple wrapper script which aquires parameters and passes them to java jar. 我有一个非常简单的包装器脚本,它获取参数并将其传递给Java jar。 Unfortunatly, I experience very-very strange behaviour. 不幸的是,我遇到了非常奇怪的行为。 Below is an example. 下面是一个例子。

Command to execute script: 执行脚本的命令:

./wrapper http://localhost:8485/metrics 900 200

Script: 脚本:

#/bin/sh

/usr/java/default/bin/java -jar /usr/plugins/checkmetrics.jar $@

Java code: Java代码:

public static void main(String[] args) throws IOException {
    String metricsUrl = args[0];
    int heapWarnValue = Integer.parseInt(args[1]);
    int threadWarnValue = Integer.parseInt(args[2]);
}

Which gives me NumberFormatException: 这给了我NumberFormatException:

"xception in thread "main" java.lang.NumberFormatException: For input string: "200

But if I change command to following, everything works: 但是,如果我将命令更改为以下命令,则一切正常:

./wrapper http://localhost:8485/metrics 900 200" "

Breaks my brain, but I can't understand where I'm wrong. 伤了我的大脑,但我不明白我哪里错了。 Could someone explain? 有人可以解释吗? Thanks in advance 提前致谢

Is there an LF or CR character at the end of the script that is not being correctly processed (could happen if you have unix line endings in a windows environment or vice versa)? 脚本末尾是否有未正确处理的LF或CR字符(如果您在Windows环境中使用Unix行尾,反之亦然)?

the reason I mention this is that the error you mention says that it is 我提到此问题的原因是您提到的错误是

For input string: "200 对于输入字符串:“ 200

I'm willing to bet that there is another quote mark at the start of the next line. 我愿意打赌,在下一行的开头还有另一个引号。 If that's the case, it's trying to parse 200 and CR together as an integer. 如果真是这样,它将尝试将200和CR一起解析为整数。 Sort out the line endings and all will be fine. 整理行尾,一切都会好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM