打开一个新的命令提示符,并从命令提示符中运行的Java程序发送命令
我有一个Java程序在命令提示符(a)中运行,我想打开一个新的命令提示符(B),在B中运行一些命令,然后关闭B命令提示符窗口 我一直在使用这些帖子作为参考: 这是我的密码:打开一个新的命令提示符,并从命令提示符中运行的Java程序发送命令,java,command-prompt,processbuilder,Java,Command Prompt,Processbuilder,我有一个Java程序在命令提示符(a)中运行,我想打开一个新的命令提示符(B),在B中运行一些命令,然后关闭B命令提示符窗口 我一直在使用这些帖子作为参考: 这是我的密码: public static void startCMD(String appName) { // init shell String[] cmd = new String[]{"cmd.exe", "/C", "start"}; Proces
public static void startCMD(String appName) {
// init shell
String[] cmd = new String[]{"cmd.exe", "/C", "start"};
ProcessBuilder builder = new ProcessBuilder(cmd);
Process p = null;
try {
p = builder.start();
} catch (IOException e) {
System.out.println(e);
}
// get stdin of shell
BufferedWriter p_stdin = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
// execute the desired command (here: ls) n times
try {
// single execution
p_stdin.write("C:");
p_stdin.newLine();
p_stdin.flush();
p_stdin.write("cd C:\\" + appName);
p_stdin.newLine();
p_stdin.flush();
p_stdin.write("sbt \"run 8080\"");
p_stdin.newLine();
p_stdin.flush();
} catch (IOException e) {
System.out.println(e);
}
// finally close the shell by execution exit command
try {
p_stdin.write("exit");
p_stdin.newLine();
p_stdin.flush();
} catch (IOException e) {
System.out.println(e);
}
// write stdout of shell (=output of all commands)
Scanner s = new Scanner(p.getInputStream());
while (s.hasNext()) {
System.out.println(s.next());
}
s.close();
}
上面的代码将打开一个新的命令提示符(B),但在此新的命令提示符(B)中未输入任何命令。当前Java程序只是挂起在命令提示符(A)中
我知道我丢失了一些命令和参数,以便在流程完成时关闭命令提示符(B)
有没有更好的方法来构建它
我感谢你的帮助