请考虑以下代码:
String commandf = "ls /etc | grep release";
try {
// Execute the command and wait for it to complete
Process child = Runtime.getRuntime().exec(commandf);
child.waitFor();
// Print the first 16 bytes of its output
InputStream i = child.getInputStream();
byte[] b = new byte[16];
i.read(b, 0, b.length);
System.out.println(new String(b));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
程序的输出是:
/etc:
adduser.co
当然,当我从外壳运行时,它可以按预期工作:
poundifdef@parker:~/rabbit_test$ ls /etc | grep release
lsb-release
互联网告诉我,由于管道行为不是跨平台的事实,在生产Java的Java工厂工作的才华横溢的人无法保证管道能正常工作。
我该怎么做?
我不会使用Java构造而不是grep
和sed
来完成所有解析,因为如果我想更改语言,将被迫用该语言重新编写解析代码,完全不行。
调用shell命令时如何使Java做管道和重定向?
编写脚本,然后执行脚本而不是单独的命令。
管道是外壳的一部分,因此您也可以执行以下操作:
String[] cmd = {
"/bin/sh",
"-c",
"ls /etc | grep release"
};
Process p = Runtime.getRuntime().exec(cmd);
我在Linux中遇到了类似的问题,除了它是“ ps -ef | grep someprocess”。至少使用“ ls”,您可以使用与语言无关的Java替代品(尽管速度较慢)。例如:
File f = new File("C:\\");
String[] files = f.listFiles(new File("/home/tihamer"));
for (String file : files) {
if (file.matches(.*some.*)) { System.out.println(file); }
}
使用“ ps”,会有点困难,因为Java似乎没有针对它的API。
我听说Sigar可以帮助我们:https://support.hyperic.com/display/SIGAR/Home
然而,最简单的解决方案(由Kaj指出)是将管道命令作为字符串数组执行。这是完整的代码:
try {
String line;
String[] cmd = { "/bin/sh", "-c", "ps -ef | grep export" };
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader in =
new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = in.readLine()) != null) {
System.out.println(line);
}
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
关于为什么String数组与管道一起使用,而单个字符串却不起作用的原因……这是宇宙的奥秘之一(特别是如果您还没有阅读源代码的话)。我怀疑这是因为给exec一个字符串时,它首先解析了它(以我们不喜欢的方式)。相反,当给exec一个字符串数组时,它只是将其传递给操作系统而不进行解析。
实际上,如果我们在忙碌的一天中抽出时间来查看源代码,(在http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Runtime.java#Runtime.exec%28java.lang.String%2Cjava.lang.String[]%2Cjava.io.File%29),我们发现这正是发生的情况:
public Process [More ...] exec(String command, String[] envp, File dir)
throws IOException {
if (command.length() == 0)
throw new IllegalArgumentException("Empty command");
StringTokenizer st = new StringTokenizer(command);
String[] cmdarray = new String[st.countTokens()];
for (int i = 0; st.hasMoreTokens(); i++)
cmdarray[i] = st.nextToken();
return exec(cmdarray, envp, dir);
}
创建运行时以运行每个进程。从第一个运行时获取OutputStream,然后从第二个运行时将其复制到InputStream中。
@@ Kaj接受的答案适用于Linux。这对于Windows是等效的:
String[] cmd = {
"cmd",
"/C",
"dir /B | findstr /R /C:"release""
};
Process p = Runtime.getRuntime().exec(cmd);