Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an executable and pass 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) {   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.

like image 947
Lorenzo B Avatar asked Apr 09 '11 11:04

Lorenzo B


People also ask

How do I pass an executable command line argument?

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.

How do I add parameters to an exe?

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.

How do I pass a variable in Command Prompt?

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.

How do I run an EXE from a batch file?

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.


2 Answers

Pass your arguments in constructor itself.

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start(); 
like image 183
Prince John Wesley Avatar answered Sep 29 '22 16:09

Prince John Wesley


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...).

like image 24
T.J. Crowder Avatar answered Sep 29 '22 18:09

T.J. Crowder