90

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.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • 1
    new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start(); – Prince John Wesley Apr 09 '11 at 11:24
  • 2
    http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html – Paweł Dyda Apr 09 '11 at 11:25
  • 1
    `new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();` Is this snippet valuable for option arguments? For example -h that stands for help and so on.. – Lorenzo B Apr 09 '11 at 12:27
  • what about paths with blank spaces? Thank you again. Very helpful!! – Lorenzo B Apr 09 '11 at 12:28
  • 3
    @Flex: An argument is an argument. The *meaning* of the argument is entirely up to the program being run. One program could interpret `-h` to be an option, another could interpret it as a filename. `ProcessBuilder` neither knows nor cares. Re paths with spaces: That's one of the reasons that arguments are given as discrete strings rather than as (say) a space-delimited list as on the command-line and in some poor APIs. Throw spaces in there with abandon, the whole program path will be given to the OS to treat as appropriate. – T.J. Crowder Apr 09 '11 at 12:41

5 Answers5

124

Pass your arguments in constructor itself.

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").start();
Prince John Wesley
  • 62,492
  • 12
  • 87
  • 94
9

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

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • For clarification: `ProcessBuilder#command(java.util.List)` and `ProcessBuilder#command(String...)` owerwrite any existing commands specified via the constructor (imho that's not very clear from the docs). – domids Nov 13 '18 at 11:27
0
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"); 
    }
}
Pang
  • 9,564
  • 146
  • 81
  • 122
0

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();
} 

**

0

I built a utility for the same, very basic one.

    public class CallOsExeUtil {
            public static String call(String[] args) throws IOException, InterruptedException {
                    ProcessBuilder processBuilder = new ProcessBuilder(args);
                    Process process = processBuilder.start();
                    int waitFlag = process.waitFor();// Wait to finish application execution.
                    StringBuilder sb = new StringBuilder("");
                    if (waitFlag == 0) {
                            if (process.exitValue()==0) {
                                    System.out.println("This is me " + process.info());

                                    BufferedInputStream in = (BufferedInputStream) process.getInputStream();
                                    byte[] contents = new byte[1024];

                                    int bytesRead = 0;

                                    while ((bytesRead = in.read(contents)) != -1) {
                                            sb.append(new String(contents, 0, bytesRead));
                                    }
                            }

                    }
                    return sb.toString();
            }
    }
connectoram
  • 81
  • 1
  • 4
  • Hey, does "new processBuilder" interpret pipe "|" or it only executes one binary with arguments ? I mean can you add pipes in the arguments list ? I am asking is can the input be given from website and any malicious user executes other commands in it ? – user1887464 Jun 05 '22 at 04:36
  • If we don't invoke bash -c command in argument list it will not interpret pipe right ? – user1887464 Jun 05 '22 at 04:37