2

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.

Rao
  • 20,781
  • 11
  • 57
  • 77

1 Answers1

3

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

Marimuthu Madasamy
  • 13,126
  • 4
  • 30
  • 52
  • I 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? – Rao Sep 09 '17 at 02:57
  • 1
    That specific syntax only works in shell script not for command line. You could, of course, run those separately. – Marimuthu Madasamy Sep 09 '17 at 03:07
  • I 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. – Rao Sep 09 '17 at 03:08
  • 2
    Another 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 it `BAR_OPTS` and set it just once so you're not running two commands every time. – Marimuthu Madasamy Sep 09 '17 at 03:22
  • I 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 Sep 09 '17 at 03:25
  • Ok, can't think of better way. I'll take it. – Rao Sep 09 '17 at 03:37