2

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.

2 Answers 2

3

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?

Sign up to request clarification or add additional context in comments.

Comments

2

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)

2 Comments

Have a solution for the UNIX-like system already. Need one for Windows as well (see title).
@bcash: Yeah, so you probably want to build a launcher/wrapper app that can then launch the actual apps

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.