具有并发输入/输出流的Java进程

具有并发输入/输出流的Java进程,java,process,inputstream,outputstream,Java,Process,Inputstream,Outputstream,我试图创建一种控制台/终端,允许用户输入一个字符串,然后输入一个进程并打印结果。就像一个普通的控制台。但是我在管理输入/输出流时遇到了问题。我已经研究过了,但遗憾的是,这个解决方案不适用于我的问题 除了“ipconfig”和“cmd.exe”等标准命令外,如果脚本请求输入,我还需要能够运行脚本并使用相同的inputstream来传递一些参数 例如,在运行脚本“python pyScript.py”之后,如果脚本需要,我应该能够向脚本传递进一步的输入(例如:raw_input),同时还可以打印脚本

我试图创建一种控制台/终端,允许用户输入一个字符串,然后输入一个进程并打印结果。就像一个普通的控制台。但是我在管理输入/输出流时遇到了问题。我已经研究过了,但遗憾的是,这个解决方案不适用于我的问题

除了“ipconfig”和“cmd.exe”等标准命令外,如果脚本请求输入,我还需要能够运行脚本并使用相同的inputstream来传递一些参数

例如,在运行脚本“python pyScript.py”之后,如果脚本需要,我应该能够向脚本传递进一步的输入(例如:raw_input),同时还可以打印脚本的输出。终端的基本行为

到目前为止,我得到的是:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class Console extends JFrame{

    JTextPane inPane, outPane;
    InputStream inStream, inErrStream;
    OutputStream outStream;

    public Console(){
        super("Console");
        setPreferredSize(new Dimension(500, 600));
        setLocationByPlatform(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // GUI
        outPane = new JTextPane();
        outPane.setEditable(false);
        outPane.setBackground(new Color(20, 20, 20));
        outPane.setForeground(Color.white);
        inPane = new JTextPane();
        inPane.setBackground(new Color(40, 40, 40));
        inPane.setForeground(Color.white);
        inPane.setCaretColor(Color.white);

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(outPane, BorderLayout.CENTER);
        panel.add(inPane, BorderLayout.SOUTH);

        JScrollPane scrollPanel = new JScrollPane(panel);

        getContentPane().add(scrollPanel);

        // LISTENER
        inPane.addKeyListener(new KeyListener(){
            @Override
            public void keyPressed(KeyEvent e){
              if(e.getKeyCode() == KeyEvent.VK_ENTER){
                    e.consume();
                    read(inPane.getText());
                }
            }
            @Override
            public void keyTyped(KeyEvent e) {}

            @Override
            public void keyReleased(KeyEvent e) {}
        });


        pack();
        setVisible(true);
    }

    private void read(String command){
        println(command);

        // Write to Process
        if (outStream != null) {
            System.out.println("Outstream again");
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outStream));
            try {
                writer.write(command);
                //writer.flush();
                //writer.close();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

        // Execute Command
        try {
            exec(command);
        } catch (IOException e) {}

        inPane.setText("");
    }

    private void exec(String command) throws IOException{
        Process pro = Runtime.getRuntime().exec(command, null);

        inStream = pro.getInputStream();
        inErrStream = pro.getErrorStream();
        outStream = pro.getOutputStream();

        Thread t1 = new Thread(new Runnable() {
            public void run() {
                try {
                    String line = null;
                    while(true){
                        BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
                        while ((line = in.readLine()) != null) {
                            println(line);
                        }
                        BufferedReader inErr = new BufferedReader(new InputStreamReader(inErrStream));
                        while ((line = inErr.readLine()) != null) {
                            println(line);
                        }
                        Thread.sleep(1000);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();
    }

    public void println(String line) {
        Document doc = outPane.getDocument();
        try {
            doc.insertString(doc.getLength(), line + "\n", null);
        } catch (BadLocationException e) {}
    }

    public static void main(String[] args){
        new Console();
    }
}
我不使用前面提到的
ProcessBuilder
,因为我喜欢区分错误流和正常流

更新日期:2016年8月29日

在@ArcticLord的帮助下,我们实现了原始问题中提出的要求。 现在只需要解决任何奇怪的行为,比如非终止过程。控制台有一个“停止”按钮,它只调用pro.destroy()。但由于某些原因,这不适用于无限运行的进程,即垃圾输出

控制台:

InputStreamLineBuffer:

不停止的代码示例:

public class Infinity{
    public static void main(String[] args){ 
        while(true){
            System.out.println(".");
        }
    }
}
停止的示例代码:

import java.util.concurrent.TimeUnit;

public class InfinitySlow{
    public static void main(String[] args){ 
        while(true){
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(".");
        }
    }
}

您使用代码的方式是正确的。你只错过了一些小事情。
让我们从您的
read
方法开始:

private void read(String command){
    [...]
    // Write to Process
    if (outStream != null) {
        [...]
        try {
            writer.write(command + "\n");  // add newline so your input will get proceed
            writer.flush();  // flush your input to your process
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    // ELSE!! - if no outputstream is available
    // Execute Command
    else {
        try {
            exec(command);
        } catch (IOException e) {
            // Handle the exception here. Mostly this means
            // that the command could not get executed
            // because command was not found.
            println("Command not found: " + command);
        }
    }
    inPane.setText("");
}
现在,让我们修复您的
exec
方法。您应该使用单独的线程来读取正常进程输出和错误输出。此外,我还引入了第三个线程,它等待进程结束并关闭outputStream,因此下一个用户输入不是针对进程的,而是一个新命令

private void exec(String command) throws IOException{
    Process pro = Runtime.getRuntime().exec(command, null);

    inStream = pro.getInputStream();
    inErrStream = pro.getErrorStream();
    outStream = pro.getOutputStream();

    // Thread that reads process output
    Thread outStreamReader = new Thread(new Runnable() {
        public void run() {
            try {
                String line = null;
                BufferedReader in = new BufferedReader(new InputStreamReader(inStream));                        
                while ((line = in.readLine()) != null) {
                    println(line);                       
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("Exit reading process output");
        }
    });
    outStreamReader.start();

    // Thread that reads process error output
    Thread errStreamReader = new Thread(new Runnable() {
        public void run() {
            try {
                String line = null;           
                BufferedReader inErr = new BufferedReader(new InputStreamReader(inErrStream));
                while ((line = inErr.readLine()) != null) {
                    println(line);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("Exit reading error stream");
        }
    });
    errStreamReader.start();

    // Thread that waits for process to end
    Thread exitWaiter = new Thread(new Runnable() {
        public void run() {
            try {
                int retValue = pro.waitFor();
                println("Command exit with return value " + retValue);
                // close outStream
                outStream.close();
                outStream = null;
            } catch (InterruptedException e) {
                e.printStackTrace(); 
            } catch (IOException e) {
                e.printStackTrace();
            } 
        }
    });
    exitWaiter.start();
}
现在应该可以了。
如果输入
ipconfig
,它将打印命令输出,关闭输出流并准备好执行新命令。
如果您输入
cmd
,它将打印输出并允许您输入更多cmd命令,如
dir
cd
等,直到您输入
exit
。然后,它关闭输出流并准备执行新命令。

您可能会在执行python脚本时遇到问题,因为如果进程输入流未刷新到系统管道中,则使用Java读取它们时会出现问题。
请参见此python脚本示例

print "Input something!"
str = raw_input()
print "Received input is : ", str
您可以使用Java程序运行此操作,也可以输入输入,但在脚本完成之前,您不会看到脚本输出。
我能找到的唯一修复方法是手动刷新脚本中的输出

import sys
print "Input something!"
sys.stdout.flush()
str = raw_input()
print "Received input is : ", str
sys.stdout.flush()
运行此脚本将如您所期望的那样成功。
有关此问题的更多信息,请访问

编辑:对于Python脚本的
stdout.flush()
问题,我刚刚找到了另一个非常简单的解决方案。使用
python-uscript.py启动它们,您不需要手动刷新。这应该能解决你的问题

EDIT2:我们在评论中讨论过,使用此解决方案,输出和错误流将混合在一起,因为它们在不同的线程中运行。这里的问题是,当出现错误流线程时,我们无法区分输出写入是否完成。否则,带有锁的经典线程调度可以处理这种情况。但无论数据是否流动,我们都有一个连续的流,直到过程结束。因此,我们需要一种机制来记录从每个流读取最后一行以来经过的时间。

为此,我将介绍一个类,该类获取InputStream并启动一个线程来读取传入数据。该线程将每一行存储在队列中,并在流结束时停止。此外,它还保存读取最后一行并将其添加到队列中的时间

public class InputStreamLineBuffer{
    private InputStream inputStream;
    private ConcurrentLinkedQueue<String> lines;
    private long lastTimeModified;
    private Thread inputCatcher;
    private boolean isAlive;

    public InputStreamLineBuffer(InputStream is){
        inputStream = is;
        lines = new ConcurrentLinkedQueue<String>();
        lastTimeModified = System.currentTimeMillis();
        isAlive = false;
        inputCatcher = new Thread(new Runnable(){
            @Override
            public void run() {
                StringBuilder sb = new StringBuilder(100);
                int b;
                try{
                    while ((b = inputStream.read()) != -1){  
                        // read one char
                        if((char)b == '\n'){
                            // new Line -> add to queue
                            lines.offer(sb.toString());
                            sb.setLength(0); // reset StringBuilder
                            lastTimeModified = System.currentTimeMillis();
                        }
                        else sb.append((char)b); // append char to stringbuilder
                    }
                } catch (IOException e){
                    e.printStackTrace();
                } finally {
                    isAlive = false;
                }
            }});
    }
    // is the input reader thread alive
    public boolean isAlive(){
        return isAlive;
    }
    // start the input reader thread
    public void start(){
        isAlive = true;
        inputCatcher.start();
    }
    // has Queue some lines
    public boolean hasNext(){
        return lines.size() > 0;
    }
    // get next line from Queue
    public String getNext(){
        return lines.poll();
    }
    // how much time has elapsed since last line was read
    public long timeElapsed(){
        return (System.currentTimeMillis() - lastTimeModified);
    }
}
也许这不是一个完美的解决方案,但它应该处理好这里的情况


编辑(2016年8月31日)
我们在评论中讨论过,在实现停止按钮时,代码仍然存在问题,该按钮会终止已启动的程序 使用
处理#销毁()
进行处理。在无限循环中产生大量输出的过程将 通过调用
destroy()
立即销毁。但由于它已经产生了大量必须消耗的产出 通过我们的
streamReader
我们无法恢复正常的程序行为。
因此,我们需要在这里进行一些小的更改:

我们将为
InputStreamLineBuffer
引入一个
destroy()
方法,该方法停止输出读取并清除队列。
更改如下所示:

public class InputStreamLineBuffer{
    private boolean emergencyBrake = false;
    [...]
    public InputStreamLineBuffer(InputStream is){
        [...]
                while ((b = inputStream.read()) != -1 && !emergencyBrake){
                    [...]
                }
    }
    [...]

    // exits immediately and clears line buffer
    public void destroy(){
        emergencyBrake = true;
        lines.clear();
    }
}
主程序也有一些小改动

public class ExeConsole extends JFrame{
    [...]
    // The line buffers must be declared outside the method
    InputStreamLineBuffer outBuff, errBuff; 
    public ExeConsole{
        [...]
        btnStop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                 if(pro != null){
                      pro.destroy();
                      outBuff.destroy();
                      errBuff.destroy();
                 }
        }});
    }
    [...]
    private void exec(String command) throws IOException{
        [...]
        //InputStreamLineBuffer outBuff = new InputStreamLineBuffer(inStream);
        //InputStreamLineBuffer errBuff = new InputStreamLineBuffer(inErrStream);        
        outBuff = new InputStreamLineBuffer(inStream);
        errBuff = new InputStreamLineBuffer(inErrStream);    
        [...]
    }
}
现在,它甚至可以销毁一些输出垃圾邮件进程。

注意:我发现
进程#destroy()
无法销毁子进程。因此,如果在windows上启动
cmd
然后从那里启动一个java程序,当java程序仍在运行时,您将破坏
cmd
进程。 您将在任务管理器中看到它。这个问题无法用java本身解决。它需要
一些操作系统依赖外部工具获取这些进程的PID并手动终止它们。

尽管@ArticLord solution很不错,但最近我遇到了同样的问题,并提出了一个概念上相当的解决方案,但在实现上略有不同

这一概念是相同的,即“批量读取”:当读线程获得轮次时,它会消耗它处理的所有流,并且只有在完成时才会出手。
这保证了输出/错误打印顺序

但我没有使用基于计时器的转弯分配,而是使用基于锁的非阻塞读取模拟:

// main method for testability: replace with private void exec(String command)
public static void main(String[] args) throws Exception
{
    // create a lock that will be shared between reader threads
    // the lock is fair to minimize starvation possibilities
    ReentrantLock lock = new ReentrantLock(true);

    // exec the command: I use nslookup for testing on windows 
    // because it is interactive and prints to stderr too
    Process p = Runtime.getRuntime().exec("nslookup");

    // create a thread to handle output from process (uses a test consumer)
    Thread outThread = createThread(p.getInputStream(), lock, System.out::print);
    outThread.setName("outThread");
    outThread.start();

    // create a thread to handle error from process (test consumer, again)
    Thread errThread = createThread(p.getErrorStream(), lock, System.err::print);
    errThread.setName("errThread");
    errThread.start();

    // create a thread to handle input to process (read from stdin for testing purpose)
    PrintWriter writer = new PrintWriter(p.getOutputStream());
    Thread inThread = createThread(System.in, null, str ->
    {
        writer.print(str);
        writer.flush();
    });
    inThread.setName("inThread");
    inThread.start();

    // create a thread to handle termination gracefully. Not really needed in this simple
    // scenario, but on a real application we don't want to block the UI until process dies
    Thread endThread = new Thread(() ->
    {
        try
        {
            // wait until process is done
            p.waitFor();
            logger.debug("process exit");

            // signal threads to exit
            outThread.interrupt();
            errThread.interrupt();
            inThread.interrupt();

            // close process streams
            p.getOutputStream().close();
            p.getInputStream().close();
            p.getErrorStream().close();

            // wait for threads to exit
            outThread.join();
            errThread.join();
            inThread.join();

            logger.debug("exit");
        }
        catch(Exception e)
        {
            throw new RuntimeException(e.getMessage(), e);
        }
    });
    endThread.setName("endThread");
    endThread.start();

    // wait for full termination (process and related threads by cascade joins)
    endThread.join();

    logger.debug("END");
}

// convenience method to create a specific reader thread with exclusion by lock behavior
private static Thread createThread(InputStream input, ReentrantLock lock, Consumer<String> consumer)
{
    return new Thread(() ->
    {
        // wrap input to be buffered (enables ready()) and to read chars
        // using explicit encoding may be relevant in some case
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        // create a char buffer for reading
        char[] buffer = new char[8192];

        try
        {
            // repeat until EOF or interruption
            while(true)
            {
                try
                {
                    // wait for your turn to bulk read
                    if(lock != null && !lock.isHeldByCurrentThread())
                    {
                        lock.lockInterruptibly();
                    }

                    // when there's nothing to read, pass the hand (bulk read ended)
                    if(!reader.ready())
                    {
                        if(lock != null)
                        {
                            lock.unlock();
                        }

                        // this enables a soft busy-waiting loop, that simultates non-blocking reads
                        Thread.sleep(100);
                        continue;
                    }

                    // perform the read, as we are sure it will not block (input is "ready")
                    int len = reader.read(buffer);
                    if(len == -1)
                    {
                        return;
                    }

                    // transform to string an let consumer consume it
                    String str = new String(buffer, 0, len);
                    consumer.accept(str);
                }
                catch(InterruptedException e)
                {
                    // catch interruptions either when sleeping and waiting for lock
                    // and restore interrupted flag (not necessary in this case, however it's a best practice)
                    Thread.currentThread().interrupt();
                    return;
                }
                catch(IOException e)
                {
                    throw new RuntimeException(e.getMessage(), e);
                }
            }
        }
        finally
        {
            // protect the lock against unhandled exceptions
            if(lock != null && lock.isHeldByCurrentThread())
            {
                lock.unlock();
            }

            logger.debug("exit");
        }
    });
}
//可测试性的主要方法:replac
// main method for testability: replace with private void exec(String command)
public static void main(String[] args) throws Exception
{
    // create a lock that will be shared between reader threads
    // the lock is fair to minimize starvation possibilities
    ReentrantLock lock = new ReentrantLock(true);

    // exec the command: I use nslookup for testing on windows 
    // because it is interactive and prints to stderr too
    Process p = Runtime.getRuntime().exec("nslookup");

    // create a thread to handle output from process (uses a test consumer)
    Thread outThread = createThread(p.getInputStream(), lock, System.out::print);
    outThread.setName("outThread");
    outThread.start();

    // create a thread to handle error from process (test consumer, again)
    Thread errThread = createThread(p.getErrorStream(), lock, System.err::print);
    errThread.setName("errThread");
    errThread.start();

    // create a thread to handle input to process (read from stdin for testing purpose)
    PrintWriter writer = new PrintWriter(p.getOutputStream());
    Thread inThread = createThread(System.in, null, str ->
    {
        writer.print(str);
        writer.flush();
    });
    inThread.setName("inThread");
    inThread.start();

    // create a thread to handle termination gracefully. Not really needed in this simple
    // scenario, but on a real application we don't want to block the UI until process dies
    Thread endThread = new Thread(() ->
    {
        try
        {
            // wait until process is done
            p.waitFor();
            logger.debug("process exit");

            // signal threads to exit
            outThread.interrupt();
            errThread.interrupt();
            inThread.interrupt();

            // close process streams
            p.getOutputStream().close();
            p.getInputStream().close();
            p.getErrorStream().close();

            // wait for threads to exit
            outThread.join();
            errThread.join();
            inThread.join();

            logger.debug("exit");
        }
        catch(Exception e)
        {
            throw new RuntimeException(e.getMessage(), e);
        }
    });
    endThread.setName("endThread");
    endThread.start();

    // wait for full termination (process and related threads by cascade joins)
    endThread.join();

    logger.debug("END");
}

// convenience method to create a specific reader thread with exclusion by lock behavior
private static Thread createThread(InputStream input, ReentrantLock lock, Consumer<String> consumer)
{
    return new Thread(() ->
    {
        // wrap input to be buffered (enables ready()) and to read chars
        // using explicit encoding may be relevant in some case
        BufferedReader reader = new BufferedReader(new InputStreamReader(input));

        // create a char buffer for reading
        char[] buffer = new char[8192];

        try
        {
            // repeat until EOF or interruption
            while(true)
            {
                try
                {
                    // wait for your turn to bulk read
                    if(lock != null && !lock.isHeldByCurrentThread())
                    {
                        lock.lockInterruptibly();
                    }

                    // when there's nothing to read, pass the hand (bulk read ended)
                    if(!reader.ready())
                    {
                        if(lock != null)
                        {
                            lock.unlock();
                        }

                        // this enables a soft busy-waiting loop, that simultates non-blocking reads
                        Thread.sleep(100);
                        continue;
                    }

                    // perform the read, as we are sure it will not block (input is "ready")
                    int len = reader.read(buffer);
                    if(len == -1)
                    {
                        return;
                    }

                    // transform to string an let consumer consume it
                    String str = new String(buffer, 0, len);
                    consumer.accept(str);
                }
                catch(InterruptedException e)
                {
                    // catch interruptions either when sleeping and waiting for lock
                    // and restore interrupted flag (not necessary in this case, however it's a best practice)
                    Thread.currentThread().interrupt();
                    return;
                }
                catch(IOException e)
                {
                    throw new RuntimeException(e.getMessage(), e);
                }
            }
        }
        finally
        {
            // protect the lock against unhandled exceptions
            if(lock != null && lock.isHeldByCurrentThread())
            {
                lock.unlock();
            }

            logger.debug("exit");
        }
    });
}