37

I tried to make an application that calls an external program that I have to pass two parameters. It doesn't give any errors.

The program.exe, written in C++, takes a picture and modifies the content of a .txt file.

The Java program runs but it does nothing-

Here is my sample code:

    String[] params = new String [3];
    params[0] = "C:\\Users\\user\\Desktop\\program.exe";
    params[1] = "C:\\Users\\user\\Desktop\\images.jpg";
    params[2] = "C:\\Users\\user\\Desktop\\images2.txt";
    Runtime.getRuntime().exec(params);
Lii
  • 10,777
  • 7
  • 58
  • 79
sqtd
  • 445
  • 1
  • 4
  • 6
  • 6
    What's the problem exactly, do you have an error message you can add to the question? Thanks. – Jonathan Dec 21 '12 at 13:27
  • you said, you want to pass two parameter, but here you are showing 3 parameters – Ravi Dec 21 '12 at 13:30
  • if *it does not* execute, what is the error ? what does your program.exe do ? – vels4j Dec 21 '12 at 13:31
  • 11
    Why is this question closed as "too localized"? – user886079 Nov 09 '15 at 15:54
  • Does this answer your question? [Java Programming: call an exe from Java and passing parameters](https://stackoverflow.com/questions/5604698/java-programming-call-an-exe-from-java-and-passing-parameters) – Dave Jarvis Apr 10 '21 at 22:05

4 Answers4

70

borrowed this shamely from here

Process process = new ProcessBuilder("C:\\PathToExe\\MyExe.exe","param1","param2").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);
}

More information here

Other issues on how to pass commands here and here

Evan Knowles
  • 7,290
  • 2
  • 34
  • 69
Steven
  • 1,275
  • 2
  • 13
  • 28
  • 1
    my problem is to pass parameters to program.exe – sqtd Dec 21 '12 at 13:49
  • Apart from getInputStream you can use getErrorStream to get the error messages (stderr) – golimar Jan 13 '15 at 14:01
  • For people looking for the one-liner with parameters: `(new ProcessBuilder(new String[]{ notePadPath, theFile.getPath()})).start();` ... well... wrapped in a try/catch.... assuming `notePadPath` is something like `c:\\program files\\notepad++\\notepad++.exe` .. double slash for escape.. and your awesome because you use np++ – Pimp Trizkit Aug 25 '15 at 00:10
  • 1
    What is the "args" variable? – user64141 Apr 15 '16 at 19:46
14

You might also try its more modern cousin, ProcessBuilder:

Java Runtime.getRuntime().exec() alternatives

Dave Jarvis
  • 29,586
  • 38
  • 176
  • 304
duffymo
  • 299,921
  • 44
  • 364
  • 552
2
import java.io.*;

public class Code {
  public static void main(String[] args) throws Exception {
    ProcessBuilder builder = new ProcessBuilder("ls", "-ltr");
    Process process = builder.start();

    StringBuilder out = new StringBuilder();
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
        String line = null;
      while ((line = reader.readLine()) != null) {
        out.append(line);
        out.append("\n");
      }
      System.out.println(out);
    }
  }
}

Try online

Vishal
  • 18,919
  • 19
  • 75
  • 92
0

This version is a Java 17 version using some built in convenience functions like the xyzReader methods and the streaming API consumption of the output. Take note that the example works for programs not running long and returning immediately when ready, so no daemons (otherwise the output processing should be done in a thread while waiting for the process to exit.).

If all the exception handling is ignored the code is really short:

        Process process = new ProcessBuilder("program", "param1", "param2").start();
        // check your program's used exit code in case or error
        if (process.waitFor() != 0) {
            throw new IOException("Program failed.");
        }
        String out;
        try (BufferedReader reader = process.inputReader()) {
            out = reader.lines().collect(Collectors.joining());
        }

A more verbose version with proper error handling:

        Process process;
        try {
            process = new ProcessBuilder("myprogram", "param1",
                    "param2").start();
            int errorCode = process.waitFor();
// adjust the error code for your use case, different programs migth not use 0 as success
            if (errorCode != 0) {
                try (BufferedReader reader = process.errorReader(StandardCharsets.UTF_8)) {
                    throw new RuntimeException(String.format("Program execution failed (code %d): %s", errorCode,
                            reader.lines().collect(Collectors.joining())));
                }
            }
        } catch (IOException e) {
            throw new RuntimeException("Could not invoke program.", e);
        } catch (InterruptedException e) {
            throw new RuntimeException("Could not wait for process exit.", e);
        }
        String output;
        try (BufferedReader reader = process.inputReader()) {
            output = reader.lines().collect(Collectors.joining());
        } catch (IOException e) {
            throw new RuntimeException("Could not invoke external program.", e);
        }

Also take into account that maybe not the inputReader is used for all output, but also the errorReader holds some information of the program output. It can also be that programs are using the inputReader for the error messages instead of the errorReader

k_o_
  • 4,014
  • 1
  • 27
  • 39