我意识到下面的长单行代码看起来很荒谬,但请将其视为伪代码。我有一个脚本连续执行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行,并且只在前一个进程被执行后才执行每一行?
答案 0 :(得分:0)
创建一个调用批处理文件的.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文件看起来像这样调用批处理文件,并且未显示运行批处理的命令提示符。
Dim WinScriptHost
设置WinScriptHost = CreateObject(" WScript.Shell")
Dim oFSO
设置oFSO = CreateObject(" Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
WinScriptHost.Run Chr(34)& sScriptDir& " \的run.bat" &安培; Chr(34),0
设置WinScriptHost = Nothing