使用 Java,在同一个 cmd.exe 窗口中运行多个命令 [英] With Java, run multiple commands in the same cmd.exe window
问题描述
我正在开发一个偶尔会在 Windows 计算机上运行的 Java 应用程序.在某些时候,我需要运行 Cygwin 提示符并在其中执行一些命令.
I'm developing a Java application that will be run on a Windows computer occasionally. At some point I need to run a Cygwin prompt and performs some commands in it.
我发现了一个使用 Runtime 类的主题:http://www.javaquery.com/2011/03/how-to-execute-multiple-command-in.html
I've found a topic where the Runtime class is used: http://www.javaquery.com/2011/03/how-to-execute-multiple-command-in.html
但是它不会启动真正的 cmd.exe 窗口,它只在后台运行,并且输出只是打印在 Eclipse 控制台上.
However it doesn't launch a real cmd.exe window, it's only run in background and the output is just printed on the Eclipse console.
我正在寻找一种解决方案来运行一个真正的 cmd.exe 窗口,我需要向该 Windows shell 传递尽可能多的命令.这可能吗?
I'm looking for a solution to run a real cmd.exe window and I need to pass as many commands as I want to that windows shell. Is this possible?
推荐答案
这个有效...使用 &&运算符,您可以添加一个或多个要在同一命令提示符下执行的命令
This one works... using && operator you can add one or commands to be executed in same command prompt
try {
Process p = Runtime
.getRuntime()
.exec("cmd /c start cmd.exe /K "dir && ping localhost && echo end"");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
考虑此处中的解决方案
来自提问者的更新:在cygwin中执行命令的解决方案
getRuntime().exec("cmd /c start C:/cygwin64/bin/bash.exe --login -c "ls ; whoami ; exec bash"");
这篇关于使用 Java,在同一个 cmd.exe 窗口中运行多个命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!