Java进程无法通过Runtime

Java进程无法通过Runtime

本文介绍了Java进程无法通过Runtime.getRunTime().exec()获得InputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try {

        String str;
        Process process = Runtime.getRuntime().exec("bash /home/abhishek/workspace/Pro/run");
        InputStream isout = process.getInputStream();
        InputStreamReader isoutr = new InputStreamReader(isout);
        BufferedReader brout = new BufferedReader(isoutr);
        while ((str = brout.readLine()) != null) {
            System.out.println(str);
        }

} catch (IOException e) {
        e.printStackTrace();
}

该代码存在从流程获取InputStream的问题,因为如果我从我的终端运行Shell脚本,它将完全正常运行,但是如果我这样运行脚本,则str始终为null,

The Code has issues with getting the InputStream from the Process,because if I run the Shell script from my Terminal it runs completely fine,but if I Run the Script like this,the str is always null,

我正在使用此代码将Shell脚本的输出直接获取到Java中,而不是将Script输出写入文件中

I am using this code to get the output of the Shell Script directly into Java instead writing the Script Output in the File

还有其他方法可以实现这一目标吗?或者如何使用当前方法解决问题

Is there any other way to achieve this,or how can I get the issue solved using the current approach

推荐答案

我认为错误流返回了某些内容,因此您可以尝试从 Process.getErrorStream()中检查某些内容.

I think something returned through the error stream, so you can try to check something from the Process.getErrorStream().

您还应该等待创建的进程,以防止主程序在此之前完成.使用 Process.waitFor();

You should also wait for the created process to prevent your main program completes before it. Use Process.waitFor();

public class TestMain {
   private static final String BASH_CMD = "bash";

   private static final String PROG = "/home/abhishek/workspace/Pro/run";

   private static final String[] CMD_ARRAY = { BASH_CMD , PROG };

   public static void main(String[] args) {
      new Thread(new Runnable() {
         public void run() {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                  System.in));
            String command = null;
            try {
               while ((command = reader.readLine()) != null) {
                  System.out.println("Command Received:" + command);
               }
            } catch (Exception ex) {
               ex.printStackTrace();
               // failed to listening command
            }

         }
      }).start();
      Process process = null;
      try {
         ProcessBuilder processBuilder = new ProcessBuilder(CMD_ARRAY);
         process = processBuilder.start();
         InputStream inputStream = process.getInputStream();
         setUpStreamGobbler(inputStream, System.out);

         InputStream errorStream = process.getErrorStream();
         setUpStreamGobbler(errorStream, System.err);

         System.out.println("never returns");
         process.waitFor();
      } catch (IOException e) {
         throw new RuntimeException(e);
      } catch (InterruptedException e) {
         throw new RuntimeException(e);
      }
   }

   public static void setUpStreamGobbler(final InputStream is, final PrintStream ps) {
      final InputStreamReader streamReader = new InputStreamReader(is);
      new Thread(new Runnable() {
         public void run() {
            BufferedReader br = new BufferedReader(streamReader);
            String line = null;
            try {
               while ((line = br.readLine()) != null) {
                  ps.println("process stream: " + line);
               }
            } catch (IOException e) {
               e.printStackTrace();
            } finally {
               try {
                  br.close();
               } catch (IOException e) {
                  e.printStackTrace();
               }
            }
         }
      }).start();
   }
}

这篇关于Java进程无法通过Runtime.getRunTime().exec()获得InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 11:14