使用 Java,在同一个 cmd.exe 窗口中运行多个命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24405681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
With Java, run multiple commands in the same cmd.exe window
提问by singe3
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.
我正在开发一个偶尔会在 Windows 计算机上运行的 Java 应用程序。在某些时候,我需要运行 Cygwin 提示符并在其中执行一些命令。
I've found a topic where the Runtime class is used: http://www.javaquery.com/2011/03/how-to-execute-multiple-command-in.html
我找到了一个使用 Runtime 类的主题:http: //www.javaquery.com/2011/03/how-to-execute-multiple-command-in.html
However it doesn't launch a real cmd.exewindow, it's only run in background and the output is just printed on the Eclipse console.
但是它不会启动一个真正的cmd.exe窗口,它只在后台运行并且输出只是打印在 Eclipse 控制台上。
I'm looking for a solution to run a real cmd.exewindow and I need to pass as many commands as I want to that windows shell. Is this possible?
我正在寻找一个解决方案来运行一个真正的cmd.exe窗口,我需要向那个 windows shell 传递尽可能多的命令。这可能吗?
采纳答案by AJJ
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();
}
Consider the solution in herealso
考虑该解决方案在这里也
Update from the questioner: Solution to execute commands in cygwin
来自提问者的更新:在cygwin中执行命令的解决方案
getRuntime().exec("cmd /c start C:/cygwin64/bin/bash.exe --login -c \"ls ; whoami ; exec bash\"");
回答by AntJavaDev
Not quite sure , but if i properly understand your problem , try : for windows at java conifguration panel, there should be un-ticked the show console button.
不太确定,但如果我正确理解您的问题,请尝试:对于 Java 配置面板上的 Windows,应该取消勾选显示控制台按钮。
回答by Serge Ballesta
If you do not need to show a console on the screen, that is easy. You have some simple steps to follow :
如果您不需要在屏幕上显示控制台,那很容易。您可以遵循一些简单的步骤:
- start a
Process
via `Process cmd = new ProcessBuilder("cmd.exe").start(); - send your commands to
cmd.getOutputStream()
- read the result of the commands from
cmd.getInputStream()
and/orcmd.getErrorStream()
- when finished with it close
cmd.getOutputStream()
, and if necessary kill the process bycmd.destroy()
- 启动一个
Process
通过`过程CMD =新的ProcessBuilder( “cmd.exe的”)开始(); - 将您的命令发送到
cmd.getOutputStream()
- 从
cmd.getInputStream()
和/或读取命令的结果cmd.getErrorStream()
- 完成后关闭
cmd.getOutputStream()
,并在必要时通过cmd.destroy()
Optionnaly, you can have output and error stream to be merged :
Optionnaly,您可以合并输出和错误流:
Process cmd = new ProcessBuilder("cmd.exe").redirectErrorStream(true).start();
then you simply ignore cmd.getErrorStream()
and only read from cmd.getInputStream()
那么你只需忽略cmd.getErrorStream()
并只读取cmd.getInputStream()