Java Process无法通过Runtime.getRunTime()。exec()获取InputStream

时间:2013-02-06 11:09:57

标签: java linux shell runtime bufferedreader

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();
}

Code有从进程中获取InputStream的问题, 因为如果我从我的终端运行Shell脚本它运行完全正常, 但如果我像这样运行脚本,str总是为空,

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

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

5 个答案:

答案 0 :(得分:5)

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

您还应该等待创建的进程阻止主程序在它之前完成。使用 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();
   }
}

答案 1 :(得分:0)

可能的问题是,当您获得inputStream时,子进程尚未就绪

尝试

Process process = Runtime.getRuntime().exec("bash /home/abhishek/workspace/Pro/run");
InputStream isout = process.getInputStream();    
process.waitFor()

答案 2 :(得分:0)

如果它是一个shell,请编辑你/home/abhishek/workspace/Pro/run并在顶部添加以下行。

#!/usr/bin/bash

并为/home/abhishek/workspace/Pro/run提供必要的执行权限。

然后使用以下行

Process process = Runtime.getRuntime().exec("/home/abhishek/workspace/Pro/run");

现在,如果运行程序打印了任何内容,您应该在输出中看到它。

答案 3 :(得分:0)

您的代码看起来很好。因此,我认为问题出在您使用的命令行(bash /home/abhishek/workspace/Pro/run)或您的脚本本身。

我建议您执行以下步骤:

  1. 尝试运行一些众所周知的命令而不是脚本。例如pwd。检查从输入流中读取的代码是否正常工作。
  2. 现在尝试简化您的脚本。创建只运行相同run1的脚本pwd。现在从java运行这个脚本,看看它是否正常工作。顺便说一下,您不必将其作为bash yourscript运行。您可以直接运行它而不需要bash前缀
  3. 如果所有这些工作开始逐步从简单到真实的脚本。我相信你会发现你的错误。可能你的脚本无法启动某些与环境相关的问题。

答案 4 :(得分:0)

尝试这样的事情:

String[] runCommand = new String[3];

runCommand[0] = "sh";
runCommand[1] = "-c";
runCommand[2] = "bash /home/abhishek/workspace/Pro/run";

Process process = runtime.exec(runCommand);
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
reader.close();
相关问题