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) { System.out.println(line); }
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.
Open a command prompt (Windows+R, type "cmd" and hit enter). Then change to the directory housing your executable ("cd enter-your-directory-here"), and run the command with the parameters.
To add launch parameters to the shortcut, click or tap inside the Target text field, and type all the arguments you want to add to it, at the end of the line. Each of the additional launch parameters must be preceded by a blank space and a hyphen.
Enclose the variable=value clause in parentheses. If the parameter contains any characters treated specially by your operating system (such as parentheses in Windows NT), enclose the entire parameter within double quotes and escape any embedded double quotes to pass them through the operating system.
Create Batch File to Run EXESave your file with the file extension . bat , e.g. run-exe-program. bat and double click on it to run the .exe program.
Pass your arguments in constructor itself.
Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();
You're on the right track. The two constructors accept arguments, or you can specify them post-construction with ProcessBuilder#command(java.util.List)
and ProcessBuilder#command(String...)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With