如何使用 Java Runtime 执行多个命令行?
How can I execute several command lines using Java Runtime?
我意识到下面的长单行代码看起来很可笑,但请将其视为伪代码。我有一个连续执行 11 个命令行的脚本。我已经使用 AutoItX(类似于 Selenium)成功地实现了它,但这样做实际上只是一个表面级别的解决方案。模拟的按钮点击为错误和中断留下了空间,而且感觉不像是干净的代码。我希望命令行本身使用 Java 运行 时间环境来执行脚本。
sendCommand("cmd cd " + homepath + "\" + a + "&&" + "mvn archetype:generate -DarchetypeCatalog=file://"+ homepath + "/.m2/repository" + "&&" + "1" + "&&" + c + "&&" + b + "&&" + c + "&&" + uuid.toString() + "&&" + "Y" + "&&" + "cd " + homepath +"\"+ a +"\" + b + "&&" + "mvn clean install" + "&&" + "cd " + homepath +"\" + a + "&&" + "cd " + homepath +"\" + a +"\" + b + "\" + b + "-plugin" + "\target" + "&&" + "jar -xvf " + zipDirectory + "&&" + "cmd cd " + homepath +"\" + a +"\" + b + "\" + b + "-plugin" + "\target\" + "\META-INF\maven\" + c + "\" + b + "-plugin" + "&&" + "copy pom.xml " + pluginDirectory + "&&" + "cd " + pluginDirectory + "&&" + "rename pom.xml " + b + "-plugin-1.0.0.pom" + "&&" + "color 0a");
private static void sendCommand(String text) throws IOException, InterruptedException {
Runtime.getRuntime().exec(text);
}
ProcessBuilder 可以接受两个以上的命令行吗?我以前从未见过它以这种方式使用。例如,这里有两行:
sendCommand("homepath/plugins", "mvn", "archetype:generate", "-DarchetypeCatalog=file://homepath/.m2/repository");
private static void sendCommand(String workingDirectory, String... command) throws IOException {
Process proc = new ProcessBuilder(command).directory(new File(workingDirectory)).start();
int status = proc.waitFor();
if (status != 0) {
// Handle non-zero exit code, which means the command failed
}
}
如何在同一个进程中运行 11 行,并在前一行执行完后才执行每一行?
创建调用批处理文件的.vbs 文件。在批处理文件中写入所有命令。
从进程构建器调用 .vbs 文件。
String ExecutionPath = "pathTo/run.vbs";
final Process process = Runtime.getRuntime().exec("wscript " + ExecutionPath);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
process.destroy();
}
}));
process.waitFor();
您的 .vbs 文件看起来像这样调用批处理文件和命令提示符 运行 批处理未显示。
昏暗的 WinScriptHost
设置 WinScriptHost = CreateObject("WScript.Shell")
昏暗的 oFSO
设置 oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
WinScriptHost.Run Chr(34) & sScriptDir & "\run.bat" & Chr(34), 0
设置 WinScriptHost = Nothing
我意识到下面的长单行代码看起来很可笑,但请将其视为伪代码。我有一个连续执行 11 个命令行的脚本。我已经使用 AutoItX(类似于 Selenium)成功地实现了它,但这样做实际上只是一个表面级别的解决方案。模拟的按钮点击为错误和中断留下了空间,而且感觉不像是干净的代码。我希望命令行本身使用 Java 运行 时间环境来执行脚本。
sendCommand("cmd cd " + homepath + "\" + a + "&&" + "mvn archetype:generate -DarchetypeCatalog=file://"+ homepath + "/.m2/repository" + "&&" + "1" + "&&" + c + "&&" + b + "&&" + c + "&&" + uuid.toString() + "&&" + "Y" + "&&" + "cd " + homepath +"\"+ a +"\" + b + "&&" + "mvn clean install" + "&&" + "cd " + homepath +"\" + a + "&&" + "cd " + homepath +"\" + a +"\" + b + "\" + b + "-plugin" + "\target" + "&&" + "jar -xvf " + zipDirectory + "&&" + "cmd cd " + homepath +"\" + a +"\" + b + "\" + b + "-plugin" + "\target\" + "\META-INF\maven\" + c + "\" + b + "-plugin" + "&&" + "copy pom.xml " + pluginDirectory + "&&" + "cd " + pluginDirectory + "&&" + "rename pom.xml " + b + "-plugin-1.0.0.pom" + "&&" + "color 0a");
private static void sendCommand(String text) throws IOException, InterruptedException {
Runtime.getRuntime().exec(text);
}
ProcessBuilder 可以接受两个以上的命令行吗?我以前从未见过它以这种方式使用。例如,这里有两行:
sendCommand("homepath/plugins", "mvn", "archetype:generate", "-DarchetypeCatalog=file://homepath/.m2/repository");
private static void sendCommand(String workingDirectory, String... command) throws IOException {
Process proc = new ProcessBuilder(command).directory(new File(workingDirectory)).start();
int status = proc.waitFor();
if (status != 0) {
// Handle non-zero exit code, which means the command failed
}
}
如何在同一个进程中运行 11 行,并在前一行执行完后才执行每一行?
创建调用批处理文件的.vbs 文件。在批处理文件中写入所有命令。 从进程构建器调用 .vbs 文件。
String ExecutionPath = "pathTo/run.vbs";
final Process process = Runtime.getRuntime().exec("wscript " + ExecutionPath);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
public void run() {
process.destroy();
}
}));
process.waitFor();
您的 .vbs 文件看起来像这样调用批处理文件和命令提示符 运行 批处理未显示。
昏暗的 WinScriptHost
设置 WinScriptHost = CreateObject("WScript.Shell")
昏暗的 oFSO
设置 oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
WinScriptHost.Run Chr(34) & sScriptDir & "\run.bat" & Chr(34), 0
设置 WinScriptHost = Nothing