有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

processbuilder无法使用java打开exe应用程序

我正在尝试使用java打开Prom(进程挖掘工具)。但它根本没有效果

try {
        new ProcessBuilder("c:\\Program Files\\Prom\\prom.exe").start() ;

                } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }  

此代码无效

但当我打开uninst时。exe在同一个文件夹中使用相同的代码,它可以完美地工作

 try {
        new ProcessBuilder("c:\\Program Files\\Prom\\uninst.exe").start() ;

                } catch (Exception e) {
        System.out.println(e);
        e.printStackTrace();
    }  

我不知道为什么会这样。有什么解决办法吗? java是否无法加载繁重的应用程序


共 (1) 个答案

  1. # 1 楼答案

    您应该通过Process.getInputStream()Process.getErrorStream()检查程序输出,因为程序可能会发出警告或错误消息,这不会导致异常。这些错误或警告通常是因为缺少路径、环境变量、对文件和文件夹的权限或缺少参数

       Process proc = new ProcessBuilder(
                      "c:\\Program Files\\Prom\\prom.exe").start() ;
    
       BufferedReader stdInput = new BufferedReader(new 
             InputStreamReader(proc.getInputStream()));
    
        BufferedReader stdError = new BufferedReader(new 
             InputStreamReader(proc.getErrorStream()));
    
        // read the output from the command
        System.out.println("Here is the standard output of the command:\n");
        while ((s = stdInput.readLine()) != null) {
            System.out.println(s);
        }
    
        // read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            System.out.println(s);
        }
    

    还应始终使用Process.exitValue()检查流程的返回代码。按照惯例,零意味着一切正常