简体   繁体   中英

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. 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:

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:

"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)?

the reason I mention this is that the error you mention says that it is

For input string: "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. Sort out the line endings and all will be fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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