Lorenzo Boaro Lorenzo Boaro - 1 year ago 115
Java Question

Java Programming: call an exe from Java and passing parameters

I'm figuring out a mechanism to call an exe from Java and passing in specific parameters. How can I do?

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe").start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

System.out.printf("Output of running %s is:", Arrays.toString(args));

while ((line = br.readLine()) != null) {

The previous code works. But I'm not able to pass parameters in. MyExe.exe accepts parameters. An other problem is when PathToExe has blank spaces. ProcessBuilder seems not working. For example:

C:\\User\\My applications\\MyExe.exe

Thank you.

Answer Source

Pass your arguments in constructor itself.

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download