问题
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.
回答1:
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).
回答2:
Simple way:-
yourclassName=JavaClassName
java `cat myOptionsFile.txt` $yourclassName
In windows, this should work fine.
set /p VAR=<myOptionsFile.txt
java.exe %VAR% yourclassName
来源:https://stackoverflow.com/questions/45942772/how-can-i-pass-java-command-line-options-in-a-separate-file