如何让管道命令也返回 Java exec 中的输出?

发布于 2024-12-14 13:14:18 字数 1003 浏览 1 评论 0原文

当我应用命令命令时,我没有得到任何输出。如何在使用命令或两种情况下都不获得输出?

public class test 
{

  public static void main(String args[])
  {
    System.out.println(
        systemtest("ifconfig | awk 'BEGIN { FS = \"\n\"; RS = \"\" } { print $1 $2 }' | sed -e 's/ .*inet addr:/,/' -e 's/ .*//'"));   

  }

  public static String systemtest(String cmds)
  {
    String value = "";
    try 
    { 
      String cmd[] = {
        "/bin/sh",
        "-c",
        cmds
      };       
      Process p=Runtime.getRuntime().exec(cmd); 
      // Try 0: here? wrong
      //p.waitFor(); 
      BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
      String line=reader.readLine(); 
      // Try 1: here?
      //p.waitFor(); 
      while(line!=null) 
      { 
        value += line + "\n";
        line=reader.readLine(); 
      } 
      // Try 2: here?
      p.waitFor(); 
    } catch(IOException e1) {

    } catch(InterruptedException e2) {

    } 

    return value;
  }

When i apply command command i do not get any output. How can i get output while using command or not both case?

public class test 
{

  public static void main(String args[])
  {
    System.out.println(
        systemtest("ifconfig | awk 'BEGIN { FS = \"\n\"; RS = \"\" } { print $1 $2 }' | sed -e 's/ .*inet addr:/,/' -e 's/ .*//'"));   

  }

  public static String systemtest(String cmds)
  {
    String value = "";
    try 
    { 
      String cmd[] = {
        "/bin/sh",
        "-c",
        cmds
      };       
      Process p=Runtime.getRuntime().exec(cmd); 
      // Try 0: here? wrong
      //p.waitFor(); 
      BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
      String line=reader.readLine(); 
      // Try 1: here?
      //p.waitFor(); 
      while(line!=null) 
      { 
        value += line + "\n";
        line=reader.readLine(); 
      } 
      // Try 2: here?
      p.waitFor(); 
    } catch(IOException e1) {

    } catch(InterruptedException e2) {

    } 

    return value;
  }

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

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

发布评论

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

评论(2

你怎么这么可爱啊 2024-12-21 13:14:18

您需要在调用 p.waitFor() 之前读取命令的输出。

事实:

  • 外部命令的输出是通过操作系统级“管道”传递的。
  • 管道的缓冲量是有限的。
  • 如果某个生产者进程尝试写入“已满”的管道,则该进程将被迫等待,直到消费者进程从管道中读取了一些数据。

不难看出(从上述事实),如果外部应用程序产生的输出多于管道缓冲区(在操作系统空间中)所能容纳的输出,那么应用程序的编写方式将导致死锁。

(即使这不是这次问题的真正原因,其他时候也可能是这样。在读取/写入外部进程时请注意死锁。)

You need to read the output from the command before you call p.waitFor().

Facts:

  • Output from an external command is delivered via an OS-level "pipe".
  • Pipes have a limited amount of buffering.
  • If some producer process tries to write to a pipe that is "full", the process is forced to wait until the consumer process has read some data from the pipe.

It is not hard to see (from the above facts) that the way that you're application is written will result in a deadlock if the external application produces more output than can fit into the pipe's buffer (in O/S space).

(Even if this is not the real cause of your problem this time, it could be other times. Watch out for deadlocks when reading from / writing to external processes.)

静若繁花 2024-12-21 13:14:18

您需要了解如何处理 stdout、stderr 和流管道。
或者,您可能有兴趣查看 Commons ExecCommons CLI
我希望它能帮助你。
祝你好运!

You need to understand how to handle stdout, stderr and stream piping.
Alternatively, you might be interested to take a look on Commons Exec and Commons CLI
I hope it will help you.
Good luck!

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