如何在单独的文件中传递Java命令行选项?

如何在单独的文件中传递Java命令行选项?

问题描述:

有什么方法可以启动Oracle的Java.exe,并使其从Windows上的文本文件中获取其命令行选项吗?

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

其中, myOptionsFile.txt包含标准Java VM选项的列表,例如 -classpath = my very long classpath和- Dthis = that等。

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

运行Java.exe时,所有选项都必须存在,因为它是名为VMMap的内存调试工具的目标。VMMap允许您启动.exe并附加到它以调试本机堆问题。在这种情况下,我无法在运行时生成命令行args或让另一个进程为我启动java.exe。

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.

您可以使用 xargs

给出一个文件 arguments.in ,内容如下:

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

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

这样调用:

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

产生此输出(即与调用 java -version ):

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)

注意:这是假设您使用的是* nix操作系统或正在使用Cygwin(或诸如 GOW )。

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