これは、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 を実行する必要があります。
サブプロセスのbinファイルを呼び出し、IOによる制御実行をそれらのbinファイルに送るためにJavaを学びたいです。
この hello.exe のように、hello world が出力され、値を入力できる場合は、"I bypass error" を確認できるのは私だけです。
これは私の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<<<<<
Hello Worldすら表示されません
また、この部分で私を助けてください:
exitVal=process.waitFor();
このように、プロセスの実行を一時停止する一時停止コマンドがあります