Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
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?

share|improve this question
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 Answers

up vote 1 down vote accepted

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 Tread() {
                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);
            }
share|improve this answer
i make the result float and it equals: -1.0737415E9 – rmaster 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 5: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 – rmaster May 28 '10 at 11:18

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

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

Other than that looks fine.

share|improve this answer
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

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|improve this answer
no, it writes to the console, some float numbers – rmaster 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

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);
    }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.