I am running a jar file using a very basic shell script (I literally just need this to run on startup of a NAS). However, I am getting some rather unexpected behavior:

Script looks like this:

java -jar myJar.war --inputParam=10101

myJar starts up - then throws a NumberFormatException saying that "10101 is not a valid number format. The question is: where is that " coming from? Anything I can try to avoid this behavior?

The exact same command works if I run it manually without a script since there's no " appearing.

Thank you kindly!

EDIT: hexdump of the original file:

0000000   /   u   s   r   /   l   o   c   a   l   /   j   r   e   /   b
0000010   i   n   /   j   a   v   a       -   j   a   r       /   s   h
0000020   a   r   e   /   m   a   t   h   u   -   g   o   /   j   e   n
0000030   k   i   n   s   .   w   a   r       -   -   h   t   t   p   P
0000040   o   r   t   =   1   0   1   0   1  \r  \n 

Thanks for all the suggestions.

  • Maybe you can hack your way out of a bug by requesting java -jar myJar.war --inputParam=10101" instead (escaping the double quote character). – 41754 Aug 29 '19 at 13:28
  • 2
    hexdump -C of the script file? maybe odd line endings or invisible characters sneaked their way in – frostschutz Aug 29 '19 at 13:28
  • Your file as a DOS text file. Try converting it to a Unix text file using dos2unix (or just retype it in a fresh file in a Unix text editor). The \r at the end of the line would be part of the argument passed to java. – Kusalananda Aug 29 '19 at 13:48
  • @Kusalananda recreating it seems to have worked. Thank you! – Nico-Ben de Villiers Aug 29 '19 at 13:54

The issue is the carriage return at the end of the line in the script file (visible as \r in the output of hexdump -C). This is a DOS line-ending, but on Unix, the carriage return would be part of the argument to java.

The error message would have said "10101" (Java would quote the string that it had difficulties interpreting in the message), but since the carriage return returns the cursor to the start of the line while printing it, the last " would have been printed at the beginning of the line, which made the error message look a bit weird.

Retyping the script in a Unix text editor, or running dos2unix over the original file, would solve this.

Also note that a script should have a #!-line at the top. For example, inserting #!/bin/sh as the script's first line would make the the script run with /bin/sh as its interpreter.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.