-2

I create a bash script to read the output from another command (java -jar ...).

The result from the command is like below:

MIN,MAX
6165350,6228865

I omit the first line using head -2 | tail -1 so the output will become

6165350,6228865

And i save it to the variable called output

output=$(java -jar bundletool.jar get-size total --apks=$APKS_PATH | head -2 | tail -1)

Last, I create java file to split the output results

import java.util.*;

public class Demo {
    public static void main(String[] args) {
        String[] values = args[0].split(",");

        System.out.println(Integer.parseInt(values[0]));
        System.out.println(Integer.parseInt(values[1]));
    }
}

And below is the final bash script

#!bin/bash
output=$(java -jar bundletool.jar get-size total --apks=$APKS_PATH | head -2 | tail -1)
java Demo $output

When i run the bash, why i'm getting the error

6165350
"xception in thread "main" java.lang.NumberFormatException: For input string: "6228865
        at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
        at java.base/java.lang.Integer.parseInt(Integer.java:652)
        at java.base/java.lang.Integer.parseInt(Integer.java:770)
        at Demo.main(Demo.java:8)

I get the correct results when i try to change the output

output="6165350,6228865"

Thank you

fanjavaid
  • 1,676
  • 8
  • 34
  • 65
  • 1
    Have you checked args with `System.out.println(args[0]);` ? – DuncG Jun 08 '23 at 16:47
  • 2
    do those input lines have windows/dos line endings (`\r\n`)? the error message a) references the last number in the line (`6228865`) and b) appears to be missing the trailing double quote; (NOTE: I don't work with `java` so making a guess at this point ...) I'm wondering if a trailing `\r` would cause the trailing double quote to show up at the front of the string (ie, overprint the leading double quote)? what is the output from running (`bash` command): `typeset -p output`? – markp-fuso Jun 08 '23 at 16:54
  • 1
    Note: An easier way to omit the first line is with `sed 1d`. I agree that there is probably undesired whitespace that is causing problems. Printing the string length would help identify it as would printing the code points in hex, and a judicious use of `String::trim` would likely fix it. – David Conrad Jun 08 '23 at 16:58
  • 1
    ah, yeah, the trailing double quote is actually at the start of the line (see lambda-larry's answer) ... definitely an issue with a `\r` at the end of the line (ie, end of the 2nd number) – markp-fuso Jun 08 '23 at 17:02
  • 1
    You can eliminate your second Java program and use other unix tools to split the output onto separate lines as another step in the pipeline - `sed` and `awk` immediately come to mind... for example `echo "$output" | head -2 | tail -1 | sed 's/,/\r\n/g'` – Stephen P Jun 08 '23 at 17:32
  • Thank you, I already checked the args[0] and printed without additional either `\r`or `\n`. I believe those characters exists in my args. And solved by using `trim()`. – fanjavaid Jun 10 '23 at 02:33

3 Answers3

2

I suspect that there is a \r at the end of the last argument, which explains why Exception is spelled "xception.

You can confirm that it is the case by pipe the output to a hex dump.

java -jar bundletool.jar get-size total --apks=$APKS_PATH | head -2 | tail -1 | xxd

You can trim the \r of with tr -d '\r' in the bash part or strip the argument in the Java part.

String[] values = args[0].strip().split(",");
lambda-larry
  • 343
  • 1
  • 7
1

If there is a non-printing character there, typically the String#strip method will remove it.
String#trim may work, also.

Try the following.

public static void main(String[] args) {
    String[] values = args[0].split(",");

    System.out.println(Integer.parseInt(values[0].strip()));
    System.out.println(Integer.parseInt(values[1].strip()));
}
Reilas
  • 3,297
  • 2
  • 4
  • 17
1

Since you mentioned that your bundletool.jar produces a carriage return as line ending, I would (if you can not change the Java program to produce reasonable line endings) simply do a

nums=( $(java .... bundletool.jar ... |dos2unix|awk -F , '{print $1, $2}') )

You then have the first number in ${nums[0]} and the second one in ${nums[1]}

user1934428
  • 19,864
  • 7
  • 42
  • 87