I have this bash file:
#/bin/bash
PROP="-Dprop=foo bar"
java $PROP -jar Foo.jar
So, what I want to do here is pass a space-separated list as a System Property. But this somehow does not work:
Caused by: java.lang.ClassNotFoundException: bar
So, it seems that Bash breaks -Dprop=foo bar up into -Dprop=foo, bar. I tried everything from double quoting to escaping the space character but nothing seems to work.
Note that this is really a shell syntax question … applied to a Java use-case. These answers will work for any use-cases where you need to expand shell variables on a command line while passing embedded spaces from the shell variables to the command in its arguments.
You need to add the quotation marks around the shell script
$PROPexpansion:For multiple
-Dproperties:Yes, the multiple
-Dproperties case is cumbersome1.Embedding quotes in the shell variable (as some people suggested) does not work2.
Finally, you can pass options to the Java command using the
@filemechanism, and you could generate an @-file on the fly to inject shell parameters into it. But the end result is (IMO) liable to be worse than the problem that you are trying to fix.1 – If there is a better way to handle the multiple properties case … that actually works for
sh,bashorksh… please comment below.2 – Try it and see!