开发者

Executing in java code an external program that takes arguments [duplicate]

This question already has answers here: How to execute command with parameters? (4 answers) Closed 6 years ago.
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?


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

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

Other than that looks fine.


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


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.


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜