同时运行多个 exe 文件,并在所有 exe 文件运行完成时进行检测(JAVA)。

huangapple go评论92阅读模式
英文:

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();
    //将路径转换为可读形式并获取exe文件名
    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。

老实说,我不知道该如何做,我已经在寻找解决方案了几天。

提前感谢!

英文:

I don't think this is possible but I am trying to do it.

I am trying to execute exe files that the user introduces trough a scanner.

When they introduce them all, all exe start at the same time.

When they finish or gets closes, we get a message.

I already catch all the exe that the user introduces with this:

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

It's in spanish but it captures the route and gets the exe. The problem is executing them all and getting the message.

I tried using this:

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

doesn't work, only does the charmap.

I honestly don't know how to do it, I am working to find a solution for days.

Thanks in advance!

答案1

得分: 1

正如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");     
英文:

As Progman said, just execute Runtime.getRuntime().exec() three times with waitFor() in each one.

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

答案2

得分: 0

使用Java 8,您可以在Stream中使用ForkJoinPool

ForkJoinPool forkJoinPool = new ForkJoinPool(5); // 任何大于3的数字

forkJoinPool.submit(() ->

  Stream.of("C:\\Windows\\System32\\charmap.exe",
            "C:\\Windows\\System32\\calc.exe",
            "C:\\Windows\\System32\\colorcpl.exe")
    .parallel() // 同时运行所有任务
    .forEach(t ->
        Runtime.getRuntime().exec(t).waitFor();
    )
)
英文:

With Java8, you can use ForkJoinPool with 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();
    )
)





</details>



huangapple
  • 本文由 发表于 2020年9月16日 22:23:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63922148.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定