Runtime.exec没有运行“find”命令 [英] Runtime.exec not running the "find" command

查看:130
本文介绍了Runtime.exec没有运行“find”命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem is that, i am using Runtime.getruntime.exec() function to run my unix command on Java. But, it jumps to the end of codes while exec() command is being run. 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);
        }

...
...
...
}

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

I do not know what it is related to, may be there is a character that exec function could not run. I need your precious help. Thank you.

推荐答案

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

Your String[] parameter to Runtime.exec() is incorrect. 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).

尝试一些东西喜欢:

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

还要调用 waitFor 你在哪里做isn不合适。您需要在进程运行时读取输出,否则可能会填满Java VM和进程之间使用的I / O缓冲区。因此,在处理完输出后,将 waitFor 移动到。

Also calling waitFor where you are doing isn't appropriate. 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. So move that waitFor to after you've processed the output.

来自处理文档:


默认情况下,创建的子进程没有自己的终端或控制台。其所有标准I / O(即stdin,stdout,stderr)操作将被重定向到父进程,[...]。由于某些本机平台仅为标准输入和输出流提供有限的缓冲区大小,无法及时写入输入流或读取子进程的输出流可能导致子进程阻塞,甚至死锁。 / p>

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.

这篇关于Runtime.exec没有运行“find”命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆