Passing optional jvm arguments to java thru batch and shell scripts in command line
VM arguments should be passed before the class name otherwise the arguments will become your program arguments so your command should be:
java -cp $CLASSPATH "$@" com.foo.Bar
With the above command, you could run with ./barRunner.sh -Dkey2=value2 -Dkey1=value1
.
Now a slight issue is that all the arguments are passed as VM arguments. You may also want to pass some arguments to your program. To do that, you could do something like this:
java -cp $CLASSPATH $JVM_ARGS com.foo.Bar "$@"
With the above command, you could pass both JVM arguments and your program arguments like,
JVM_ARGS="-Dkey2=value2 -Dkey1=value1" ./barRunner.sh programArg1 programArg2
Related videos on Youtube
Rao
Updated on June 04, 2022Comments
-
Rao about 2 years
Got a java class with main method, say
com.foo.Bar
. But there are two optional vm(system) arguments for this class, say key1, key2.Created a batch & shell shell scripts to call the above programs as below respectively.
Contents of Batch File(
barRunner.cmd
)@echo off set CLASSPATH=/bla/;/bla/ java -cp %CLASSPATH% com.foo.Bar %*
Contents of Shell script(
barRunner.sh
)export CLASSPATH=/bla/:/bla/ java -cp $CLASSPATH com.foo.Bar $@
Now user calling in the below manner, but vm arguments cannot be read by Bar class
barRunner.cmd -Dkey1=value1
or
./barRunner.sh -Dkey2=value2 -Dkey1=value1
Suspect that the vm argument is passed after the class.
How to pass the vm arguments so that they are available before class name?
EDIT:
Have already tried changing script as below and it worked. But the issue if the class has program arguments.
java -cp %CLASSPATH% %* com.foo.Bar
Also aware of JAVA_OPTS, but it is a bit tedious for naive users; I mean, multiple commands to be run(set JAVA_OPTS in one command & and call the script in another line) and hesitant to use that way.
So thought of checking with forum if there is better way to achieve both vm & program arguments in single line and both args are optional.
-
Rishikesh Darandale almost 7 yearsCan you try using
JAVA_OPTS
variable before java command? -
Rishikesh Darandale almost 7 yearsSomething like this
export JAVA_OPTS=...
andjava $JAVA_OPTS ...
-
Rishikesh Darandale almost 7 yearsYou can take look at this as well.
-
Rao almost 7 years@RishikeshDarandale, aware of
JAVA_OPTS
, but it is a bit tedious; i mean, multiple commands to be run and hesitant to use that way. So thought of checking with forumn if there is better way. Sorry, I could not mentioned this in the question in hurry.
-
-
Rao almost 7 yearsI could not mention, but I tried the same way. And landing into the issue mentioned by in the later part of your answer. Are you sure, both
JVM_ARGS
and calling script in single line? -
Marimuthu Madasamy almost 7 yearsThat specific syntax only works in shell script not for command line. You could, of course, run those separately.
-
Rao almost 7 yearsI felt that I should have been more clear in the question based on the comments and answer. Just updated the question with details. By the way, thank you so much for your swift reply.
-
Marimuthu Madasamy almost 7 yearsAnother option might be to treat the first argument as special and treat that as JVM arguments but it feels hacky. A good thing about the solution in the answer is that you don't have to set the JVM args every time.
JVM_ARGS
is not special either, you could name itBAR_OPTS
and set it just once so you're not running two commands every time. -
Rao almost 7 yearsI got what you say. Actually, the utility is being created for naive users. So, exploring how can it be kept more simple so that there is no confusion and easy for them to use.
-
Rao almost 7 yearsOk, can't think of better way. I'll take it.