简体   繁体   中英

How do you include a classpath wildcard as an argument to a java bash call?

I'm trying to create a bash script that will build a list of arguments before calling java, but I am having trouble getting a classpath argument with a wild card to work. The script so far:

#!/bin/bash
PROP_FILES=./props/default.properties
#EXTRA_PROP_FILES_HERE

ARGS="-cp \"lib/*\""
ARGS="$ARGS -Xmx10G"
ARGS="$ARGS -Duser.timezone=GMT"
ARGS="$ARGS -Danl.property.files=$PROP_FILES"

java $ARGS main.class.path.Main

The problem is with the ARGS="-cp \\"lib/*\\"" . I'm debugging the file with the command bash -x runscript.sh .

If I escape the wildcard cp (like in the script above), the script fails with the output (note the extra single quotes):

java -cp "lib/*" ...

If I don't escape it, the * expands to all of the jars in the lib directory and also fails

How do I modify the script so that it calls this?

java -cp "lib/*" ...

Note : I know I could just do java -cp "lib/*" $ARGS .., , but I'd really like to get this working.

You should use an array with proper quoting:

propfiles='./props/default.properties'

args=(-cp 'lib/*')
args+=(-Xmx10G -Duser.timezone=GMT)
args+=("-Danl.property.files=$propfiles")

java "${args[@]}" main.class.path.Main

Also, using uppercase variable names makes it more likely to clash with environment variables, so it's best avoided.

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