在java代码中执行带参数的外部程序

时间:2010-05-27 13:42:48

标签: java process exec

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

我没有任何错误,只是没有。在cmd.exe中,prog.exe运行正常。

为了使此代码有效,需要改进哪些内容?

4 个答案:

答案 0 :(得分:3)

使用p = new ProcessBuilder(params).start();代替

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

除此之外看起来很好。

答案 1 :(得分:2)

也许您应该使用waitFor()来获取结果代码。这意味着标准输出的转储必须在另一个线程中完成:

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

答案 2 :(得分:1)

我刚在我的系统上试过这个:

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

它工作正常。 你确定你正在使用的程序实际打印到控制台的东西吗?我看到它需要jpegs作为输入,也许它写入文件,而不是stdout。

答案 3 :(得分:0)

就像从流程的输入流中读取一样,您也可以像这样读取错误流:

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