java - 读取 system.in 时 Runtime.exec 永远不会返回

标签 java multiprocessing stdin runtime.exec

这是我的示例代码,我想在运行新的子进程时处理来自标准输入的命令。但是,如果我读取 system.in,exec 方法永远不会返回。 exec() 中的命令非常简单,与 stdin 无关。

我想知道有什么办法可以解决这个问题吗?如何在启动另一个线程读取 stdin 的同时启动一个新的子进程?

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 {
        process = Runtime.getRuntime().exec("java -cp C:/agenttest Test");
        System.out.println("never returns");
        process.waitFor();
    } catch (IOException e) {
        throw new RuntimeException( e );
    } catch (InterruptedException e) {
        throw new RuntimeException( e );
    }
}

Test类很简单,这里是Test.java

public static void main(String[] args){
    System.out.println("Standard out");
    System.out.println("Standard out");
    System.err.println("Standard err");
    System.out.println("Standard out");
    try{
        Thread.sleep(10000);
    }catch(InterruptedException ex){}
}

最佳答案

问题可能是您没有处理错误流和输入流,并且超出了平台的缓冲区。尝试按照著名文章 When Runtime.exec() won't 处理该输出。 。

例如:

import java.io.*;

public class TestMain {
   private static final String JAVA_CMD = "java";
   private static final String CP = "-cp";

   // *** your CLASS_PATH and PROG Strings will of course be different ***
   private static final String CLASS_PATH = "C:/Users/hovercraft/Documents/workspace/Yr 2012A/bin";
   private static final String PROG = "yr12.m07.b.Test2";

   private static final String[] CMD_ARRAY = { JAVA_CMD, CP, CLASS_PATH, 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 - 读取 system.in 时 Runtime.exec 永远不会返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11732399/

相关文章:

C 禁用命令行输入

c - 从标准输入段错误中获取输入

java - JUnit 测试 - 应该通过预期结果的测试有什么问题?

java - 读取对象输入流时Java中的EOFexception

python multiprocessing apply_async 只使用一个进程

python - With 子句用于 Python 中的多处理

c - 如何使用 GLib 获取基于文本的用户输入

java - Java 与 Python 中的类方法调用(一般 OO 问题)

java - 如果表存在则跳过 liquibase 更改

python - 如何在 python 中将数据从单个 hdf5 文件安全地并行写入多个文件?