我每次重新启动子进程比我需要获取一个随机数,但我认为它可以使用outPutStream完成,但我不知道如何。我看到其他人发帖但我没有发现与我的问题无关。我不想使用套接字。
主要流程。
public class ejercicio7 {
public static void main(String [] args){
boolean cerrado = false;
try {
while(!cerrado){
String entrada = JOptionPane.showInputDialog(null, "Introduce una entrada");
Process proceso = Runtime.getRuntime().exec("java -jar C:\\Users\\Cristian\\Desktop\\java\\numeros.jar \"" + entrada+"\"");
BufferedReader br = new BufferedReader(new InputStreamReader(proceso.getInputStream()));
String texto;
while((texto = br.readLine()) !=null){
if(Boolean.parseBoolean(texto)){
cerrado = true;
}else{
System.out.println(texto);
}
}
br.close();
proceso.destroy();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
儿童过程。
public static void main(String[] args) {
if(args.length <=0 && args[0].length() == 0){
System.out.println("No se ha introducido una entrada");
}else{
if(!args[0].equalsIgnoreCase("fin")){
System.out.println(Math.round(Math.random() * 10));
}else{
System.out.println("true");
}
}
}
}
答案 0 :(得分:1)
让您的子进程处理输入流:
public static void main(String[] args) throws Exception {
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, "UTF-8"));
while (!"fin".equals(line = reader.readLine())) {
System.out.println(line);
}
System.out.println("buy");
}
然后在主进程中,您可以向子进程发送消息:
Process proceso = Runtime.getRuntime().exec("java -jar C:\\Users\\Cristian\\Desktop\\java\\numeros.jar");
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(proceso.getOutputStream()));
BufferedReader in = new BufferedReader(new InputStreamReader(proceso.getInputStream()));
String texto;
out.write(entrada + "\n");
out.flush();
while((texto = in.readLine()) !=null){
...