java - 如何在 Windows 操作系统中使用 Runtime.exex() 从单个类的单独命令窗口中调用多个 java 程序?

标签 java windows batch-file

我正在使用 Runtime.exex() 类似的东西在 2 个单独的命令窗口(在 Windows 7 环境中)中调用 2 个不同的 java 程序。

   public class Invoke{
         public void main(...){
            String[] class1 = {"start", "java", "A"}; //Assume A.java is already compiled
            String[] class2 = {"start", "java", "B"}; //Assume B.java is already compiled
            try{
            Runtime.getRuntime().exec(class1);
            Runtime.getRuntime().exec(class2);
            }catch(Exception e){
               e.printStackTrace();
             }
         }
   }

但它给了我异常(exception)

       java.io.IOException: Cannot run program "start": CreateProcess error=2, The syst
       em cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at initialConfig.StartApp.win_startProg(StartApp.java:95)
    at initialConfig.StartApp.main(StartApp.java:134)
     Caused by: java.io.IOException: CreateProcess error=2, The system cannot find th
     e file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)

我是否需要创建一个单独的 .bat 文件来调用这两个程序,然后在 Runtime.exec() 中使用这些 bat?请帮忙。谢谢。

最佳答案

我认为startcmd.exe的内部命令。所以尝试一下:

String[] class1 = {"cmd.exe", "/c", "start", "java", "A"};

这会导致在单独的进程和窗口中调用 java - 并且不会等待它终止。

关于java - 如何在 Windows 操作系统中使用 Runtime.exex() 从单个类的单独命令窗口中调用多个 java 程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19750578/

相关文章:

batch-file - 批处理 : add a unicode header or how to add hex values or any other ways around this?

Java 2D 渲染波纹工件

java - 在求解 X 时从 JTextArea 检索 double 值

c++ - 在 c++ 的 Windows 线程编程中,pthread_setcanceltype 的替代方案是什么?

ruby - Windows 上的 gem 错误 - "openpath: pathname too long"

c - 当 WFP 标注驱动程序以 SERVICE_SYSTEM_START 启动时,FwpmEngineOpen 返回 EPT_NT_CANT_PERFORM_OP

windows - 以字符串作为分隔符拆分字符串

Java - 当不需要并且不知道出了什么问题时,字段实例变量会覆盖预先存在的值

java.lang.StringIndexOutOfBoundsException : String index out of range when trying to check if a word is a palindrome

batch-file - 如果时间小于19,bat文件将调用A.bat。:45 and to call B.如果时间大于19:45 bat