0

谁能帮我让这段代码在linux中工作

    public static void main(String[] args) throws Exception,{   
    String s ="find . -exec ls -al {} \\;";
    Process exec = Runtime
            .getRuntime()
            .exec(s);
    exec.waitFor();
System.out.println(s);
    if (exec.getErrorStream().available() != 0) {
        BufferedInputStream bf = new BufferedInputStream(
                exec.getErrorStream());
        DataInputStream din = new DataInputStream(bf);
        System.out.println(din.readLine());
    }

    System.out.println(exec.exitValue());
}

我得到以下输出。

find . -exec ls -al {} \;
find: missing argument to `-exec'
1
4

2 回答 2

1

尝试使用这个:

String[] params = {"find", ".", "-exec", "ls", "-al", "{}", ";"};

而不是您的普通字符串命令。

不过要小心:输出很重(我用 /dev/. 测试过),waitFor 方法可能会导致 I/O 缓冲区爆裂。

于 2013-04-17T00:16:37.913 回答
0

反斜杠太多

find . -exec ls -al {} ';'
于 2013-04-16T23:59:32.270 回答