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

发布于 2024-09-03 08:25:29 字数 612 浏览 5 评论 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()));

    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 中工作正常。

为了使这段代码正常工作,需要改进什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

年少掌心 2024-09-10 08:25:29

使用 p = new ProcessBuilder(params).start(); 而不是

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

除此之外看起来不错。

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

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

Other than that looks fine.

从此见与不见 2024-09-10 08:25:29

也许您应该使用 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);
            }

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);
            }
雾里花 2024-09-10 08:25:29

我刚刚在我的系统上尝试过:

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

效果很好。
您确定您使用的程序确实在控制台上打印了一些内容吗?我看到它接受 jpeg 作为输入,也许它写入文件,而不是标准输出。

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.

纵山崖 2024-09-10 08:25:29

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

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

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);
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文