簡體   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