java - Why is my environment variable incorrectly processed as a JVM option? -


i want use environment variable jvm option when executing java -jar.

the command want execute it:

java -xx:onoutofmemory='echo test' -jar foo.jar 

when run above command is, jar run.

(if don't have foo.jar, error: unable access jarfile foo.jar error. still means option gets used correctly).

but when create environment variable containing jvm option, , run command using variable.

oom="-xx:onoutofmemory='echo test'" java $oom -jar foo.jar 

than following error:

error: not find or load main class test' 

it seems java command ignoring quotes around 'echo test'.

after looking similar questions on so , on other websites, tried various variations of using quotes:

oom="-xx:onoutofmemoryerror=\"echo test\"" oom='-xx:onoutofmemoryerror="echo test"' oom=-xx:onoutofmemoryerror="echo test" 

but result in same error.

an article oracle concerning jvm options, mentions using semicolon:

-xx:onoutofmemoryerror="<cmd args>; <cmd args>" 

but purpose of semicolon separate multiple commands, not command , arguments. not fix problem.

does know how can correctly move -xx:onoutofmemory='echo test' option environment variable?

when running java, should quote $oom

example:

java "$oom" -jar foo.jar 

see why shell script choke on whitespace or other special characters? on unix stackexchange why needed.


Comments