So i understand how to use the Runtime command in java pretty well to get an executable to run. My question is how would i code that to incorporate a parameter such as you would see in the target in a shortcut property, i.e. target: "C:......\notepad.exe" -w. In what way could I incorporate a parameter such as -w into the Java runtime command.
Asked
Active
Viewed 8,020 times
4 Answers
4
Use a ProcessBuilder and supply the necessary arguments to its constructor:
ProcessBuilder builder = new ProcessBuilder("C:\\path\\to\\notepad.exe", "-w");
The first argument is always the application, any other arguments (if present) will be the arguments to add to the application.
You can then call the start()
method to start it and grab the process object back if you so wish.

Michael Berry
- 70,193
- 21
- 157
- 216
-
can you help with http://stackoverflow.com/questions/43051640/call-an-exe-from-java-with-passing-parameters-with-writing-to-stdout-and-reading ? – gstackoverflow Mar 27 '17 at 16:36
1
Take a look at ProcessBuilder - http://download.oracle.com/javase/1.5.0/docs/api/java/lang/ProcessBuilder.html
This should provide you a relatively fail-safe way to execute with parameters and arguments

sgibly
- 3,828
- 1
- 21
- 21
0
In addition to the abovementioned, you can do something like:
Runtime.getRuntime().exec(exeFile.toString() + "params");
where exeFile is a File of your executable.

Stephen Wilkins
- 125
- 1
- 1
- 8
-
This isn't a great idea because you have to put the spaces in manually between arguments (causing more potential for errors) and it's not necessarily platform independent. – Michael Berry Aug 06 '11 at 10:52