I ran into a similar problem on Linux, except that it was "ps -ef | grep someprocess".
At least with "ls" you have a language-independent (albeit slow) Java replacement. For example:.
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); } }
With "ps" this is a little more complicated because Java doesn't seem to have an API for it.
I heard that Cigar could help us: https://support.hyperic.com/display/SIGAR/Home
The simplest solution, however (as indicated by Kaj) is to execute the piped command as a string array. Here is the complete code:
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(); }
As for why the String array works with pipe, while one row is not ... this is one of the mysteries of the universe (especially if you haven't read the source code). I suspect this is because when exec is given one line, it parses it first (in a way that we don't like). In contrast, when exec is given a string array, it simply passes it to the operating system without parsing it.
Actually, if we choose the time from a busy day and look at the source code ( 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 ), we find that this is exactly what happens:
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); }
Tihamer Oct. 15 '13 at 14:21 2013-10-15 14:21
source share