运行子进程,在Java中正确地提供输入和输出

前端之家收集整理的这篇文章主要介绍了运行子进程,在Java中正确地提供输入和输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用Runtime exec()方法Java中创建子进程.但是,由于子进程是一个交互式程序,我需要在需要时为它提供输入.另外,我需要显示子进程的输出.我怎样才能以最简单的方式做到这一点?

我使用StreamGobbler使用process.getInputStream()显示程序输出.但是,我不知道如何识别程序何时等待输入以及何时使用proc.getOutputStream提供输入.我怎样才能做到这一点?

解决方法

您需要复制子进程的流和系统流(System.in,System.out和System.err)之间的输入和输出.这与 my recent quesion有关.到目前为止,我找到的最佳解决方案是:
  1. import java.io.FileInputStream;
  2. import java.io.FilterInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.lang.reflect.Field;
  7. import java.nio.ByteBuffer;
  8. import java.nio.channels.AsynchronousCloseException;
  9. import java.nio.channels.FileChannel;
  10.  
  11. class StreamCopier implements Runnable {
  12. private InputStream in;
  13. private OutputStream out;
  14.  
  15. public StreamCopier(InputStream in,OutputStream out) {
  16. this.in = in;
  17. this.out = out;
  18. }
  19.  
  20. public void run() {
  21. try {
  22. int n;
  23. byte[] buffer = new byte[4096];
  24. while ((n = in.read(buffer)) != -1) {
  25. out.write(buffer,n);
  26. out.flush();
  27. }
  28. }
  29. catch (IOException e) {
  30. System.out.println(e);
  31. }
  32. }
  33. }
  34.  
  35. class InputCopier implements Runnable {
  36. private FileChannel in;
  37. private OutputStream out;
  38.  
  39. public InputCopier(FileChannel in,OutputStream out) {
  40. this.in = in;
  41. this.out = out;
  42. }
  43.  
  44. public void run() {
  45. try {
  46. int n;
  47. ByteBuffer buffer = ByteBuffer.allocate(4096);
  48. while ((n = in.read(buffer)) != -1) {
  49. out.write(buffer.array(),n);
  50. out.flush();
  51. }
  52. out.close();
  53. }
  54. catch (AsynchronousCloseException e) {}
  55. catch (IOException e) {
  56. System.out.println(e);
  57. }
  58. }
  59. }
  60.  
  61. public class Test {
  62. private static FileChannel getChannel(InputStream in)
  63. throws NoSuchFieldException,IllegalAccessException {
  64. Field f = FilterInputStream.class.getDeclaredField("in");
  65. f.setAccessible(true);
  66. while (in instanceof FilterInputStream)
  67. in = (InputStream)f.get((FilterInputStream)in);
  68. return ((FileInputStream)in).getChannel();
  69. }
  70.  
  71. public static void main(String[] args)
  72. throws IOException,InterruptedException,NoSuchFieldException,IllegalAccessException {
  73. Process process = Runtime.getRuntime().exec("sh -i +m");
  74. Thread outThread = new Thread(new StreamCopier(
  75. process.getInputStream(),System.out));
  76. outThread.start();
  77. Thread errThread = new Thread(new StreamCopier(
  78. process.getErrorStream(),System.err));
  79. errThread.start();
  80. Thread inThread = new Thread(new InputCopier(
  81. getChannel(System.in),process.getOutputStream()));
  82. inThread.start();
  83. process.waitFor();
  84. System.in.close();
  85. outThread.join();
  86. errThread.join();
  87. inThread.join();
  88. }
  89. }

这里棘手的部分是从System.in中提取一个通道.如果没有这个,你将无法在子进程终止时中断读取输入的线程.

这种方法有一个严重的缺点:关闭System.in之后,你再也无法读取它了.我目前使用的解决方法是使用单个输入重定向线程用于所有子进程.

猜你在找的Java相关文章