Runtime.exec() - 01
2. The sub process get inputting message from the main process
// testSubProcess.java
import java.io.*;
public class testSubProcess{
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("From the main process:"+ br.readLine());
}
}
//testMainProcess.java
import java.io.*;
public class testMainProcess{
public static void main(String[] args){
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("java testSubProcess");
//ProcessBuilder pb = new ProcessBuilder("java", "testSubProcess");
//Process p = pb.start();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
bw.flush();
bw.close();
}
}
// testSubProcess.java
import java.io.*;
public class testSubProcess{
public static void main(String[] args){
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("From the main process:"+ br.readLine());
}
}
//testMainProcess.java
import java.io.*;
public class testMainProcess{
public static void main(String[] args){
Runtime rt = Runtime.getRuntime();
Process p = rt.exec("java testSubProcess");
//ProcessBuilder pb = new ProcessBuilder("java", "testSubProcess");
//Process p = pb.start();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
bw.flush();
bw.close();
}
}
评论
发表评论