简体   繁体   中英

Loading VM arguments from a file when running a Java application on Windows

I want to be able to run my Java program VMArgumentsFromFile (detailed below) from the command line and print out "Success" in the command prompt. The command line entry should look something like this:

java <vmarguments.txt> VMArgumentsFromFile

Where the contents of vmarguments.txt is:

-Dvmopttest=Success

And the contents of VMArgumentsFromFile.java is:

public class VMArgumentsFromFile {

   public static void main(String[] args) {
      System.out.println(System.getProperty("vmopttest","Fail");
   }
}

What is proper command line entry to get this program to output "Success" on a Windows system?

There are a couple of questions close to this ( here and here ) but neither address this specific case I'm presenting. Please do not provide answers solving this from within the Java program (like sending the filename as a vm parameter and then setting its contents programmatically). In practice, the answer to this question will be used to send a number of different applications the same vm arguments which will be consolidated in one text file.

The best solution I've found today is to create the cmd file as follows:

@echo off
set LIST=
for /F %%k in (vmarguments.txt) DO call :concat %%k
echo %LIST%
java %LIST% VMArgumentsFromFile
:concat
set LIST=%LIST% %1
goto :eof

This approach requires that the vm arguments in the text file are surrounded in quotes and on their own lines.

This is based off the answer at: How to concatenate strings in a Windows batch file?

If you are on a UNIX-like system with a sane shell, you may be able to do something like:

java `cat vmarguments.txt` VMArgumentsFromFile

The backticks tell the shell to execute the command contained inside them, and replace the expression (from backtick to backtick, inclusive) with the output of that command.

Alternatively you can build a launcher app that reads the file and constructs an appropriate command line. So you would end up running something like:

launcher arguments.txt VMArgumentsFromFile

Where launcher is your launcher app (which could be a shell/batch script or another Java app)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM