tags:

views:

169

answers:

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?

A: 

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

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

Other than that looks fine.

Robert
Why should he use ProcessBuilder to execute a single process?
Michael Mrozek
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
A: 

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);
            }
Maurice Perry
i make the result float and it equals: -1.0737415E9
rmaster
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
yep, and it was error codeno need(in my case for too many thread) but now it's workingi don't know what happened but waitFor(); and Thread.sleep() was useful in investigation
rmaster
A: 

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