我想从另一个使用运行 Java 程序ProcessBuilder
我使用了代码
Process pr = rt.exec("cmd /c cd C:\\Users\\naman\\Desktop & java CalculateSum");
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
BufferedReader error = new BufferedReader(new InputStreamReader(pr.getErrorStream()));
BufferedWriter output= new BufferedWriter(new OutputStreamWriter(pr.getOutputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
output.write("10");
output.write("30");
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);
CalculateSum 有以下代码:
System.out.print("Enter 1 st value : ");
a=Integer.parseInt(br.readLine());
System.out.print("\nEnter second number : ");
b=Integer.parseInt(br.readLine());
System.out.println("\nresult is : "+(a+b));
我的基本动机是从另一个 Java 程序运行一个 Java 程序。
注意:我不想使用命令行参数来获取输入。我也尝试过使用ProcessBuilder
相同的目的,但这也没有用。