![](/img/trans.png)
[英]open a command prompt from java code and run some commands and read the cmd prompt display?
[英]Java - Open command prompt, run two commands
没问题,我敢肯定,但是我正在尝试打开命令提示符,切换到目录,然后运行第二条命令。 我有:
try {
// Execute command
String command = "cmd /c start cmd.exe";
Process child = Runtime.getRuntime().exec(command);
// Get output stream to write from it
OutputStream out = child.getOutputStream();
out.write("cd C:\Users\Me\Desktop\Reports\Scripts /r/n".getBytes());
out.flush();
out.write("for %f in (*.txt) do type "%f" >> Export.txt /r/n".getBytes());
out.close();
} catch (IOException e) {
}
我无知的猜测是第二条命令中的“%f”需要正确编写,但是我对java知之甚少,所以不确定如何执行。
您可以使用Runtime.getRuntime()。exec(“ ...”)运行命令行,也可以使用“ &&”执行多个命令,例如:
Runtime.getRuntime().exec("cmd /c \"start something.bat && start somethingelse.bat \"");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.