在JAVA中捕获外部程序的输出

AHm*_*Net 8 java exec output

我正在尝试使用java捕获外部程序的输出,但我不能.

我有代码来显示它,但不是把它放到变量中.

我将使用,例如,sqlplus执行我的oracle代码"into exec.sql"system/orcl @ orcl:user/password/DB name

public static String test_script () {
        String RESULT="";
        String fileName = "@src\\exec.sql";
        String sqlPath = ".";
        String arg1="system/orcl@orcl";
        String sqlCmd = "sqlplus";


        String arg2   = fileName;
        try {
            String line;
            ProcessBuilder pb = new ProcessBuilder(sqlCmd, arg1, arg2);
            Map<String, String> env = pb.environment();
            env.put("VAR1", arg1);
            env.put("VAR2", arg2);
            pb.directory(new File(sqlPath));
            pb.redirectErrorStream(true);
            Process p = pb.start();
          BufferedReader bri = new BufferedReader
            (new InputStreamReader(p.getInputStream()));

          while ((line = bri.readLine()) != null) {

              RESULT+=line;

          }


          System.out.println("Done.");
        }
        catch (Exception err) {
          err.printStackTrace();
        }
 return RESULT;
    }
Run Code Online (Sandbox Code Playgroud)

jsu*_*hre 9

因为进程将在新线程中执行,所以当您进入while循环时,可能没有输出或不完整的输出可用.

Process p = pb.start();  
// process runs in another thread parallel to this one

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

// bri may be empty or incomplete.
while ((line = bri.readLine()) != null) {
    RESULT+=line;
}
Run Code Online (Sandbox Code Playgroud)

因此,在尝试与其输出进行交互之前,您需要等待该过程完成.尝试使用Process.waitFor()方法暂停当前线程,直到您的进程有机会完成.

Process p = pb.start();  
p.waitFor();  // wait for process to finish then continue.

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

while ((line = bri.readLine()) != null) {
    RESULT+=line;
}
Run Code Online (Sandbox Code Playgroud)

这只是一种简单的方法,您也可以在并行运行时处理流程的输出,但是您需要监控流程的状态,即它是否仍在运行或已完成,以及输出的可用性.


Muh*_*ana 8

使用Apache Commons Exec,它将使您的生活更轻松.查看教程以获取有关基本用法的信息.要在获取executor对象(可能DefaultExecutor)之后读取命令行输出,请创建所需的OutputStream任何流(即FileOutputStream实例可能是,或System.out),以及:

executor.setStreamHandler(new PumpStreamHandler(yourOutputStream));
Run Code Online (Sandbox Code Playgroud)

  • 这是一个java库.相信我,它会为你省去很多努力.如果你遇到任何麻烦,请在这里发布,我很乐意随时帮助你:) (3认同)