Executing in java code an external program that takes arguments

19,188

Solution 1

Perhaps you should use waitFor() to obtain the result code. This means that the dump of the standard output must be done in another thread:

String path;
String[] params = new String [3];

                    params[0] = "D:\\prog.exe";
        params[1] = picA+".jpg";
        params[2] = picB+".jpg";

        try {
            final Process p = Runtime.getRuntime().exec(params);
            Thread thread = new Thread() {
                public void run() {
                    String line;
                    BufferedReader input =
                       new BufferedReader
                         (new InputStreamReader(p.getInputStream()));

                     while ((line = input.readLine()) != null)
                         System.out.println(line);


                     input.close();
               } catch (IOException e) {System.out.println(" procccess not read"+e);}
            };
            thread.start();
            int result = p.waitFor();
            thread.join();
            if (result != 0) {
                System.out.println("Process failed with status: " + result);
            }

Solution 2

Use a p = new ProcessBuilder(params).start(); instead of

p = Runtime.getRuntime().exec(params);

Other than that looks fine.

Solution 3

I just tried this on my system:

public static void main(String[] args) throws IOException {
        String[] params = { "svn", "help" };
        Process p = Runtime.getRuntime().exec(params);

        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line;
        while ((line = input.readLine()) != null) {
            System.out.println(line);
        }

        input.close();
    }

and it worked fine. Are you sure the program you're using actually prints something to the console? I see it takes jpegs as input, maybe it writes to a file, not stdout.

Share:
19,188
questioner
Author by

questioner

Whether you think you can or you think you can’t - you’re right

Updated on June 05, 2022

Comments

  • questioner
    questioner almost 2 years
    Process p;
    String line;
    String path;
    String[] params = new String [3];
    
    params[0] = "D:\\prog.exe";
    params[1] = picA+".jpg";
    params[2] = picB+".jpg";
    
    try
    {
        p = Runtime.getRuntime().exec(params);
    
        BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
    
        while ((line = input.readLine()) != null)
            System.out.println(line);
    
        input.close();
    }
    catch (IOException e)
    {
        System.out.println(" procccess not read"+e);
    }
    

    I don't get any error, just nothing. In cmd.exe prog.exe is working fine.

    What to improve in order to make this code working?

  • Michael Mrozek
    Michael Mrozek almost 14 years
    Why should he use ProcessBuilder to execute a single process?
  • questioner
    questioner almost 14 years
    no, it writes to the console, some float numbers
  • Andrei Fierbinteanu
    Andrei Fierbinteanu almost 14 years
    Most likely readLine() is called before p has actually outputted anything, and returns null, causing your program to end. You need to wait for p. p.waitFor() might work like Maurice suggested, but I tried that on my example and it just hangs at that call (doesn't return). You might also try using Thread.currentThread().wait(10); before the while loop and see if anything changes (if it does it's definitely a problem with synchronization).
  • questioner
    questioner almost 14 years
    i make the result float and it equals: -1.0737415E9
  • Robert
    Robert almost 14 years
    ProcessBuilder was written to replace Runtime.exec and makes it easier to customize the process, and provides better control over starting the process. Since there is nothing else actually wrong with his code, seemed as good an improvement as any
  • Muhammad Ahmad Afzal
    Muhammad Ahmad Afzal almost 14 years
    Why make it a float? anyway, this is the output status of prog.exe; it's signification depends on the program being run.
  • questioner
    questioner almost 14 years
    yep, and it was error code no need(in my case for too many thread) but now it's working i don't know what happened but waitFor(); and Thread.sleep() was useful in investigation