繁体   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