How can I pass Java command line options in a separate file?

3.2k Views Asked by At

Is there any way of launching Oracle's Java.exe and have it take its command line options from a text file on Windows?

What I'd like to be able to do is something like this:

java.exe -optionsFile=myOptionsFile.txt MyClass

where 'myOptionsFile.txt' contains a list of standard Java VM options such as "-classpath="my very long classpath" and "-Dthis=that" etc.

All of the options must be present when Java.exe is run as it is the target of a memory debugging tool called VMMap. VMMap allows you to launch a .exe and attach to it in order to debug native heap issues. In this case, I cannot generate the command line args at runtime or have another process launch java.exe for me.

4

There are 4 best solutions below

4
On

You could use xargs.

Given a file, arguments.in, with the following contents:

~/dev/code$ more arguments.in
-version

Invoked like so:

~/dev/code$ cat arguments.in | xargs $JAVA_HOME/bin/java

Produces this output (i.e. the same output as if you invoked java -version):

java version "1.8.0_51"
Java(TM) SE Runtime Environment (build 1.8.0_51-b16)
Java HotSpot(TM) 64-Bit Server VM (build 25.51-b03, mixed mode)

Note: this assumes you are either on a *nix OS or are using Cygwin (or a simulator emulator such as GOW).

1
On

Simple way:-

yourclassName=JavaClassName
java `cat myOptionsFile.txt` $yourclassName

In windows, this should work fine.

set /p VAR=<myOptionsFile.txt
java.exe %VAR% yourclassName
0
On

Building on top of @nagendra547's answer,

java $( grep -vE '^[#;]' "$PWD/jvm.parameters" ) -jar whatever.jar

will also allow you to include comments in your parameters file.

0
On

Since Java 11 (maybe before), it's possible to pass an argument file

$ java @my-args.txt -jar the.jar

my-args.txt

-Xms1G
-Xmx30G