这是 hello.exe 的 C++ 代码:
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<"Hello world\n";
getch();
cout<<"I bypass error\n";
return 0;
}
我需要从 Java 程序运行 hello.exe。
我想学习 Java 以便调用子进程 bin 文件并将 IO 的控制执行发送到这些 bin 文件。
就像在这个 hello.exe 中一样,它将打印 hello world,如果我能够输入该值,那么只有我能够看到“我绕过错误”。
这是我的 Java 程序:
package procs;
import java.io.*;
import java.lang.*;
import java.util.*;
public class Procs
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
try
{
int exitVal;
char c = 'a';
Process process = Runtime.getRuntime().exec(new String[]{"C:/hello.exe"});
OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
//PrintStream writer = new PrintStream(new BufferedOutputStream(stdin));
//BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
// i uses both printstream and Buffered writer
System.out.println("lets start<<<<<<");
do
{
c = (char)reader.read();
System.out.print(c);
}
while (c != '\n');
//writer.Print(c); i uses both print
//writer.write(c); and the writer
writer.flush();
do
{
c = (char)reader.read();
System.out.print(c);
}
while (c != '\n');
exitVal = process.waitFor();
System.out.println("Exited with error code " + exitVal);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
它挂起后lets start<<<<<
它甚至不显示你好世界
也帮助我完成这部分:
exitVal=process.waitFor();
像这样我们有一些暂停命令来暂停进程的执行