gpt4 book ai didi

java - Linux从java中查找命令不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 11:24:01 24 4
gpt4 key购买 nike

任何人都可以帮助我让这段代码在 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

最佳答案

尝试使用这个:

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

而不是普通的 String 命令。

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

关于java - Linux从java中查找命令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16046101/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com