英文:
Command prompt is not closing after completing even after writing exit
问题
我有以下的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");
}
批处理文件是:
cd "C:\Users\test\output\test-master"
mvn clean install -DskipTests
exit
它正在打包文件,但是命令提示符在进程完成后没有关闭。请提供建议。
英文:
I have following java code
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.
答案1
得分: 0
你应该删除包装器 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();
如果这个进程生成了大量输出,如果你不消耗错误和输出流,你可能会遇到第二个问题。你可以在后台线程中执行这些操作,或者在 pb.start()
之前通过添加以下调用将 stdout/err 发送到文件中:
pb.redirectOutput(new File(location, "std.out"));
pb.redirectError(new File(location, "std.err"));
英文:
You should remove wrapper CMD.EXE and start, just call the batch file directly as:
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. You can do this in background threads, or simply send stdout/err to files by adding these calls before pb.start()
:
pb.redirectOutput(new File(location, "std.out"));
pb.redirectError(new File(location, "std.err"));
专注分享java语言的经验与见解,让所有开发者获益!
评论