如果我在终端中运行它,则工作正常:

lsof -F n +D /some/directory


但是当我从Java运行同一件事时,它不会:

                    Process lsof = new ProcessBuilder("lsof", "-F", "n", "+D", "'/some/directory'").start();
                    lsof.waitFor();

                    if (lsof.exitValue() != 0) {
                        BufferedReader reader = null;
                        try {
                            reader = new BufferedReader(new InputStreamReader(lsof.getErrorStream()));
                            String line = null;
                            StringBuffer sb = new StringBuffer();
                            while ((line = reader.readLine()) != null) {
                                sb.append(line);
                                sb.append("\n");
                            }

                            log.warning("STDOUT:\n" + sb.toString());
                        } finally {
                            if (reader != null) {
                                reader.close();
                            }
                        }
                    }


从Java调用时,它返回:

STDOUT:
  lsof: WARNING: can't stat('/some/directory'): No such file or directory
lsof 4.85
 latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/
 latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ
 latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man
 usage: [-?abhlnNoOPRtUvV] [+|-c c] [+|-d s] [+D D] [+|-f[cgG]]
 [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+|-M] [-o [o]] [-p s]
[+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]
Use the ``-h'' option to get more help information.


谁能解释为什么?

最佳答案

new ProcessBuilder("lsof", "-F", "n", "+D", "'/some/directory'")
                                             ^               ^


删除/some/directory周围的单引号。它们被传递给lsof程序,并被解释为路径名的一部分。

10-04 17:24