簡體   English   中英

同時運行多個 exe 並檢測它們何時完成 JAVA

[英]Running more than one exe at the same time and dettect when all of them finishes JAVA

我不認為這是可能的,但我正在努力做到這一點。

我正在嘗試執行用戶通過掃描儀引入的 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);

不起作用,只有魅力圖。

老實說,我不知道該怎么做,我幾天來一直在努力尋找解決方案。

提前致謝!

正如 Progman 所說,只需執行 Runtime.getRuntime().exec() 3 次,每次執行 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與 Stream 一起使用。

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();
    )
)



暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM