Reputation: 4928
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.
Upvotes: 2
Views: 2695
Reputation: 46
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?
Upvotes: 3
Reputation: 53034
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)
Upvotes: 2