Java - 打开命令提示符,运行两个命令

时间:2015-01-20 23:47:07

标签: java command-prompt

Noobish问题我确定,但我正在尝试打开命令提示符,切换到目录,然后运行第二个命令。我有:

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知之甚少,我不知道该怎么做。

1 个答案:

答案 0 :(得分:0)

您可以使用Runtime.getRuntime()。exec(" ...")来运行命令行,您可以使用"&&"执行多个命令,例如:

Runtime.getRuntime().exec("cmd /c \"start something.bat && start somethingelse.bat \"");