在输出输出并将输入传递给在Java下运行的执行过程时出现问题

问题描述

| 我正在尝试调用一个简单的程序test.exe,就像-
int main()
{
    int a;
    cout<<\"Welcome\\n\";
    while(cin>>a&&a!=0)
    cout<<\"you entered \"<<a<<endl;
}
我想从Java程序作为一个进程运行它,并从中发送+接收I / O。我正在使用具有2个线程的进程,如下所示-
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Processproblem {

    public static void main(String[] args)throws IOException,InterruptedException {
        final Process process;
          try {
            process = Runtime.getRuntime().exec(\"test.exe\");
        } catch (IOException e1) {
            e1.printstacktrace();
            return;
        }

           new Thread(new Runnable() {
            public void run() {
                String line;
                BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                try {
                    while ((line = br.readLine()) != null) {
                        System.out.println(\"[OUT] \" + line);
                    }
                } catch (IOException e) {
                    e.printstacktrace();
                }
            }
        }).start();


      new Thread(new Runnable() {
        public void run() {
            try {
                byte[] buffer = new byte[1024];
                int reading=0;
                System.out.println(reading);
                BufferedWriter bw= new BufferedWriter(new OutputStreamWriter(process.getoutputStream()));

                while(reading!=-1)
                {
                     reading= system.in.read(buffer);
                     for(int i = 0; i < buffer.length; i++) {
                        int intValue = new Byte(buffer[i]).intValue();
                        if (intValue == 0) {
                            reading = i;
                            break;
                        }
                        else
                        {
                            bw.append((char)intValue);
                        }
                    }
                      bw.newLine();
                        bw.flush();

                }

            } catch (Exception e) {
            }
        }
    }
            ).start();
    }
}
但是他们没有按预期工作。当我运行程序时,它仅显示\“ Welcome \\ n \”消息,然后停止输入。当我给出一个整数并在Java控制台中按Enter时,它什么也不做。 我究竟做错了什么?它们是两个独立的线程,为什么它们互相阻塞?我的概念有什么问题吗?     

解决方法

程序等待您的输入。抓取进程输出流(使用
getOutputStream
)并写入。