在JAVA中运行.BAT文件而不打开命令提示符

Sim*_*n T 3 java

我一直在尝试.bat使用以下代码在Java中运行可执行文件:

Runtime.getRuntime().exec("call " + batFile);
Run Code Online (Sandbox Code Playgroud)

但它返回一个错误

无法使用commandLine启动进程nullCreateProcess:call batfilename here error = 2 IOException:Create Process:call batfilename here error = 2

我管理通过更换绕过此Stringexec()与功能"cmd /c start " + batFile,但这种打开这是不允许的命令提示.

这有变通方法吗?谢谢!

Mad*_*mer 6

尝试直接运行批处理文件,例如......

ProcessBuilder pb = new ProcessBuilder("C:/Test.bat");
pb.redirectError();
try {
    Process p = pb.start();
    try (InputStream inputStream = p.getInputStream()) {
        int in = -1;
        while ((in = inputStream.read()) != -1) {
            System.out.print((char)in);
        }
    }
    System.out.println("Exited with " + p.waitFor());
} catch (IOException | InterruptedException ex) {
    ex.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

这是批处理文件......

@echo Hello World
Run Code Online (Sandbox Code Playgroud)

(我知道,大量)和输出的代码......

Hello World
Exited with 0
Run Code Online (Sandbox Code Playgroud)