繁体   English   中英

Runtime.exec 没有运行“查找”命令

[英]Runtime.exec not running the “find” command

我的问题是,我正在使用 Runtime.getruntime.exec() function 在 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 function 无法运行的字符。 我需要你宝贵的帮助。 谢谢你。

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

尝试类似:

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

在你正在做的地方也调用waitFor是不合适的。 您需要在进程运行时读取 output,否则可能会填满 Java VM 和您的进程之间使用的 I/O 缓冲区。 因此,在您处理完 output 之后,将waitFor移至该位置。

流程文档:

默认情况下,创建的子进程没有自己的终端或控制台。 它的所有标准 I/O(即 stdin、stdout、stderr)操作都将被重定向到父进程,[...]。 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 .

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM