gpt4 book ai didi

Java,系统命令运行失败

转载 作者:行者123 更新时间:2023-12-02 08:42:03 25 4
gpt4 key购买 nike

我正在尝试运行系统命令将 tar.bz2 文件提取到指定目录。这是代码:

ProcessBuilder myProc = new ProcessBuilder("tar", "-xjf", "/path/to/MyTarFile.tar.bz2"); 
myProc.directory(new File("/directory/i/want/results/in/"));
myProc.start();
System.out.println(myProc.command());

它运行时没有错误,但是文件被删除并且没有提取到任何地方。

任何帮助将不胜感激。

最佳答案

我知道 Runtime.exec() 有 a really nasty feature如果您不手动排出 STDOUT/STDERR,它实际上会挂起。我希望 ProcessBuilder 能够纠正这个缺陷,但是 this page包括这个花絮:

A word of caution about the examples in this tip. It is possible that the examples will deadlock if the subprocess generates enough output to overflow the system. A more robust solution requires draining the process stdout and stderr in separate threads.

因此,请确保您正在处理 Process.getInputStream() 和 Process.getErrorStream(),正如我在评论中建议的那样;它可以彻底解决问题!

关于Java,系统命令运行失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3533973/

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