gpt4 book ai didi

java - Runtime.exec 未运行 "find"命令

转载 作者:行者123 更新时间:2023-12-02 00:38:26 26 4
gpt4 key购买 nike

我的问题是,我正在使用 Runtime.getruntime.exec() 函数在 Java 上运行我的 unix 命令。但是,当 exec() 命令运行时,它会跳转到代码末尾。代码如下。

    Process songProcess;
ArrayList<String> xmlFilePathsForEmi = new ArrayList<String>();
int countForEmiSongUpdates = 0;
String line;
try {
songProcess = Runtime.getRuntime().exec(new String[]{"find /home/gozenem/emiornek/ -name '*.xml'"}); // It jumps here !
songProcess.waitFor();
bufferedReaderSong = new BufferedReader(new InputStreamReader(songProcess.getInputStream()));
while((line = bufferedReaderSong.readLine()) != null){
xmlFilePathsForEmi.add(line);
}

...
...
...
}

我不知道它与什么有关,可能是有一个字符导致exec函数无法运行。我需要你宝贵的帮助。谢谢。

最佳答案

您的 Runtime.exec()String[] 参数不正确。必须将其拆分,以便每一项包含一个元素(可执行文件必须是一个字符串,然后每个单独的参数必须包含在其自己的字符串中)。

尝试如下:

songProcess = Runtime.getRuntime().exec(new String[]{"find", "/home/gozenem/emiornek/", "-name", "*.xml"});

在您正在做的地方调用 waitFor 也是不合适的。您需要在进程运行时读取输出,否则您将面临填满 Java VM 和进程之间使用的 I/O 缓冲区的风险。因此,将 waitFor 移至处理完输出之后。

来自Process文档:

By default, the created subprocess does not have its own terminal or console. All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, [...]. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

关于java - Runtime.exec 未运行 "find"命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7062595/

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