简体   繁体   English

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

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

My problem is that, i am using Runtime.getruntime.exec() function to run my unix command on Java.我的问题是,我正在使用 Runtime.getruntime.exec() function 在 Java 上运行我的 unix 命令。 But, it jumps to the end of codes while exec() command is being run.但是,它会在运行 exec() 命令时跳转到代码末尾。 The codes are below.代码如下。

    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);
        }

...
...
...
}

I do not know what it is related to, may be there is a character that exec function could not run.我不知道它与什么有关,可能是exec function 无法运行的字符。 I need your precious help.我需要你宝贵的帮助。 Thank you.谢谢你。

Your String[] parameter to Runtime.exec() is incorrect.您的Runtime.exec()String[]参数不正确。 It must be split up so that it contains one element per item (the executable must be one string, then each individual argument must come in its own string).它必须被拆分,以便每个项目包含一个元素(可执行文件必须是一个字符串,然后每个单独的参数必须在其自己的字符串中)。

Try something like:尝试类似:

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

Also calling waitFor where you are doing isn't appropriate.在你正在做的地方也调用waitFor是不合适的。 You need to read the output while the process is running, otherwise you risk filling up the I/O buffers that are used between the Java VM and your process.您需要在进程运行时读取 output,否则可能会填满 Java VM 和您的进程之间使用的 I/O 缓冲区。 So move that waitFor to after you've processed the output.因此,在您处理完 output 之后,将waitFor移至该位置。

From the Process docs:流程文档:

By default, the created subprocess does not have its own terminal or console.默认情况下,创建的子进程没有自己的终端或控制台。 All its standard I/O (ie stdin, stdout, stderr) operations will be redirected to the parent process, [...].它的所有标准 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 . 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