3
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?

jww
  • 97,681
  • 90
  • 411
  • 885
questioner
  • 2,283
  • 3
  • 26
  • 35
  • 3
    Just a small observation: Java is not C, you don't need to declare your variables before the block of code. In fact it is sometimes better to declare them inside the block, since then the garbage collector will have an easier time cleaning them up. – Andrei Fierbinteanu May 27 '10 at 13:58

4 Answers4

4

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

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

Other than that looks fine.

Robert
  • 8,406
  • 9
  • 38
  • 57
  • Why should he use ProcessBuilder to execute a single process? – Michael Mrozek May 27 '10 at 14:17
  • 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 – Robert May 27 '10 at 15:10
2

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);
            }
Frankie
  • 24,627
  • 10
  • 79
  • 121
Maurice Perry
  • 32,610
  • 9
  • 70
  • 97
  • i make the result float and it equals: -1.0737415E9 – questioner May 27 '10 at 14:51
  • Why make it a float? anyway, this is the output status of prog.exe; it's signification depends on the program being run. – Maurice Perry May 28 '10 at 05:39
  • 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 – questioner May 28 '10 at 11:18
1

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.

Andrei Fierbinteanu
  • 7,656
  • 3
  • 31
  • 45
  • no, it writes to the console, some float numbers – questioner May 27 '10 at 14:24
  • 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). – Andrei Fierbinteanu May 27 '10 at 14:37
0

Just like reading from the input stream of the process, you can also read from the error stream like this:

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

        BufferedReader error =
            new BufferedReader
              (new InputStreamReader(p.getErrorStream()));

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


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

          input.close();
          error.close();

    } catch (IOException e) {
            System.out.println(" procccess not read"+e);
    }
vkrausser
  • 393
  • 1
  • 3
  • 17
paragjain
  • 265
  • 1
  • 5
  • 12