Hey guys,
I have a program that I want to launch and pass a parameter. For example:

"C:\Program Files\aProgram\test.exe -login joeSmith password1"

The thing is I can't just add that to a shortcut because lots of other people will be using this computer as well. Is there a way of executing a program with parameters through java?


9 Years
Discussion Span
Last Post by neilcoffey

Take a look at the Runtime class. Although I have not tried it with parameters, I think all you have to do is give the entire command to execute to its exec() method and it will return you a reference to that Process.


I assume here you want to run the above command from java.
Java provides you with facilities to run commands at shell/prompt with the Runtime.exec() function
It should be something like this :

String cmd = "C:\Program Files\aProgram\test.exe -login joeSmith password1";
Runtime run = Runtime.getRuntime();
Process proc = run.exec(cmd);

Also see this (E.g. From Java Developer's Almanac)


By the way, you should be very wary of just making your program call Runtime.exec() and "sitting back and relaxing": if the other process logs anything out (to either the standard output or error stream), you must read the contents of those streams else things will generally freeze.

If you use a ProcessBuilder, you can call its redirectErrorStream() method so that you only have to read from one stream instead of two.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.