Process Builder参数

时间:2018-06-11 15:41:44

标签: java ubuntu terminal processbuilder arp

final String commands[] =  {"arp", "-n", "|" ,"grep", "98:5d:ad:3d:36:ef", "|", "awk '", "{print $1}", "'"};
ProcessBuilder pb = new ProcessBuilder(commands); 

我想在MAC ADDRESS的情况下检索IP。
当我将此命令插入终端(ubuntu 16.04)时,它可以工作。
但是当我在JAVA中使用它时它不起作用。

我做错了什么?

只有当我这样运行它时才有效:

final String commands[] =  {"arp", "-n"};
ProcessBuilder pb = new ProcessBuilder(commands); 

1 个答案:

答案 0 :(得分:2)

你需要调用“sh”并将你的管道命令传递给该程序。 尝试:

ProcessBuilder b = new ProcessBuilder( "/bin/sh", "-c",
               "arp -n | grep 98:5d:ad:3d:36:ef | awk '{print $1}'" );