有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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) 个答案

  1. # 1 楼答案

    您可以使用运行时。getRuntime()。执行官(“…”)要运行命令行,可以使用“&;&;”要执行多个命令,例如:

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