[java] 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) {
  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.

This question is related to java exe processbuilder

The answer is


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


import java.io.IOException;
import java.lang.ProcessBuilder;

public class handlingexe {
    public static void main(String[] args) throws IOException {
        ProcessBuilder p = new ProcessBuilder();
        System.out.println("Started EXE");
        p.command("C:\\Users\\AppData\\Local\\Google\\Chrome\\Application\\chrome.exe");   

        p.start();
        System.out.println("Started EXE"); 
    }
}

Below works for me if your exe depend on some dll or certain dependency then you need to set directory path. As mention below exePath mean folder where exe placed along with it's references files.

Exe application creating any temporaray file so it will create in folder mention in processBuilder.directory(...)

**

ProcessBuilder processBuilder = new ProcessBuilder(arguments);
processBuilder.redirectOutput(Redirect.PIPE);
processBuilder.directory(new File(exePath));
process = processBuilder.start();
int waitFlag = process.waitFor();// Wait to finish application execution.
if (waitFlag == 0) {
...
 int returnVal = process.exitValue();
} 

**


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to exe

How can I convert a .py to .exe for Python? Why Visual Studio 2015 can't run exe file (ucrtbased.dll)? PHP is not recognized as an internal or external command in command prompt How do I create an executable in Visual Studio 2013 w/ C++? How to my "exe" from PyCharm project How do I open an .exe from another C++ .exe? How do I convert a Python program to a runnable .exe Windows program? How can I find out if an .EXE has Command-Line Options? How to make exe files from a node.js app? The program can't start because cygwin1.dll is missing... in Eclipse CDT

Examples related to processbuilder

ProcessBuilder: Forwarding stdout and stderr of started processes without blocking the main thread Difference between ProcessBuilder and Runtime.exec() Java Programming: call an exe from Java and passing parameters How to get PID of process I've just started within java program? Executing another application from Java