簡體   English   中英

使用Runtime.getRuntime()。exec()在Java中運行Perl代碼

[英]Using Runtime.getRuntime().exec() to run perl code in java

我有一個名為pbp的perl腳本,它將一個html文件作為參數,然后創建一個輸出文件。 這是我當前的代碼。 Infile是從JFile選擇器較早獲得的。 我沒有任何錯誤,但是perl腳本沒有輸出。

try {
            Process p = Runtime.getRuntime().exec(new String[] {"perl", "C:\\Users\\Roger\\Downloads\\The_Long-Awaited_Product_Specification_and_Sample_Files\\casey\\pbp", inFile.getAbsolutePath()});
            p.getInputStream().close();
            p.getOutputStream().close();
            p.getErrorStream().close();
            System.out.println(p.waitFor());
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

您不會直接獲得每個腳本的輸出。 您需要使用以下代碼捕獲它:

BufferedReader stdInput = new BufferedReader(new 
     InputStreamReader(p.getInputStream()));


// read the output from the command
System.out.println("EXE OUTPUT");
while ((s = stdInput.readLine()) != null) {
    System.out.println(s);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM