简体   繁体   English

即使在写入退出后,命令提示符也不会在完成后关闭

[英]Command prompt is not closing after completing even after writing exit

I have following java code我有以下 java 代码

public static void main(String a[]) {
        String location = "C:\\Users\\test\\output\\testProject";
        File dir = new File("C:\\Users\\test\\cmds");
        ProcessBuilder pb = new ProcessBuilder("cmd.exe", "/C", "Start /wait","packageProject.bat",location);
        pb.directory(dir);
        Process p = null;
        try {
            p = pb.start();
            p.waitFor();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch(InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("Folder created");
    }

Batch file is批处理文件是

cd "C:\Users\test\output\test-master"

mvn clean install -DskipTests

exit

It is packaging file but not command prompt is not closing once process is complete.它是打包文件,但不是命令提示符,一旦过程完成,它就不会关闭。

Please suggest.请建议。

You should remove wrapper CMD.EXE and start, just call the batch file directly as:您应该删除包装器 CMD.EXE 并开始,只需直接调用批处理文件:

String bat = new File(dir, "packageProject.bat").getAbsolutePath();
ProcessBuilder pb = new ProcessBuilder(bat , location);
pb.directory(dir);
Process p = pb.start();
p.waitFor();

If this process generates a lot of output you may run into a second problem if you don't consume error and output streams.如果这个过程产生了大量的 output 如果你不消费错误和 output 流,你可能会遇到第二个问题。 You can do this in background threads, or simply send stdout/err to files by adding these calls before pb.start() :您可以在后台线程中执行此操作,或者通过在pb.start()之前添加这些调用来简单地将 stdout/err 发送到文件:

pb.redirectOutput(new File(location, "std.out"));
pb.redirectError(new File(location, "std.err"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM