同时运行多个exe,并在所有exe都完成JAVA时检测

同时运行多个exe,并在所有exe都完成JAVA时检测,java,Java,我认为这是不可能的,但我正在努力做到这一点 我试图执行用户通过扫描仪引入的exe文件 当他们全部介绍它们时,所有exe都会同时启动 当他们结束或接近时,我们会收到一条信息 我已经捕获了用户通过以下方式引入的所有exe: String ruta = input.nextLine(); System.out.println(ruta); if(!ruta.equals("stop")){ Syste

我认为这是不可能的,但我正在努力做到这一点

我试图执行用户通过扫描仪引入的exe文件

当他们全部介绍它们时,所有exe都会同时启动

当他们结束或接近时,我们会收到一条信息

我已经捕获了用户通过以下方式引入的所有exe:

    String ruta = input.nextLine();
        System.out.println(ruta);
        
        if(!ruta.equals("stop")){
            System.out.println("hola");
        }
        
  while(!ruta.equals("Stop") && !ruta.equals("stop")){
           
        nombreRuta.add(ruta);
     
               int length = ruta .length();
       //Convertimos la ruta para que sea legible y conseguimos el nombre del exe de la ruta
       ruta = ruta .replace("\\", "/");

     
       
       while(fin==false){

          if(  ruta.charAt((length-1)-contador)== '/' ){
              longitudexe=contador;
              fin=true;
          }
          else{
              contador++;
          }
           
       }
       
        nombreExe= ruta .substring(ruta .length()-longitudexe);
      exeRuta.add(nombreExe);
      
      
      
              System.out.print("Pon la ruta de otro exe o pon stop: ");
    input = new Scanner(System.in);
         ruta = input.nextLine();
    
  }     
   
 
       System.out.println( Arrays.toString(nombreRuta.toArray()));
它是西班牙语的,但它捕获了路由并获取exe。问题是执行它们并获取消息

我试着用这个:

 String[] COMPOSED_COMMAND = {
        "C:\\Windows\\System32\\charmap.exe",
        "C:\\Windows\\System32\\calc.exe",
        "C:\\Windows\\System32\\colorcpl.exe",};
Process p = Runtime.getRuntime().exec(COMPOSED_COMMAND);
不起作用,只有charmap起作用

老实说,我不知道该怎么做,我已经花了好几天的时间寻找解决方案


提前谢谢

正如Progman所说,只需执行Runtime.getRuntime().exec()三次,每次执行waitFor()

  Runtime.getRuntime().exec("C:\\Windows\\System32\\charmap.exe").waitFor();
              Runtime.getRuntime().exec("C:\\Windows\\System32\\dvdplay.exe").waitFor();
              Runtime.getRuntime().exec("C:\\Windows\\System32\\colorcpl.exe").waitFor();

       System.out.println("Terminadas todas");     

使用Java8,您可以将
ForkJoinPool
与流一起使用

ForkJoinPool forkJoinPool = new ForkJoinPool(5); //Any number more than 3

forkJoinPool.submit(()->

  Stream.of("C:\\Windows\\System32\\charmap.exe",
            "C:\\Windows\\System32\\calc.exe",
            "C:\\Windows\\System32\\colorcpl.exe")
    .parallel() //Run all at the same time
    .forEach( t ->
        Runtime.getRuntime().exec(t).waitFor();
    )
)




为什么你不简单地执行三次
Runtime.getRuntime().exec()
?我想了想,但是我怎么知道所有的程序都完成了呢?进程
对象有一个方法
isAlive()
你可以检查,或者直接用
waitFor()
等待它的终止。好的,我按照你的指示去做,它成功了!在所有的exec()之后,我刚刚添加了一个sout,并且工作了!谢谢你的建议!您应该先有一个循环来执行所有进程,然后是
waitFor()
,我如何知道它们是否都完成了?@syzyysyzyy.waitFor()确保所有进程都将被阻止,直到完成为止。ForkJoinPool顾名思义,它等待并在最后加入。所以,在forkJoinPoo.submit(..)之后,每个过程都将完成。