I realize the long single line code below looks ridiculous but please treat it as pseudo code. I have a script that executes 11 command lines in a row. I've successfully implemented it using AutoItX (similar to Selenium), but doing so is really only a surface-level solution. Simulated button clicks leave room for error and interruption and just doesn't feel like clean code. I want the command line itself to execute the script using the Java runtime environment.
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);
}
Can ProcessBuilder accept more than two command lines? I've never seen it used that way before. For example, here are two lines:
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
}
}
How can I run 11 lines in the same process, and execute each one only after the previous one has been executed?
Create a .vbs file which calls batch file. In batch file write all your commands. Call the .vbs file from process builder.
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();
Your .vbs file looks like this which calls the batch file and command prompt running the batch is not shown.
Dim WinScriptHost
Set WinScriptHost = CreateObject("WScript.Shell")
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
WinScriptHost.Run Chr(34) & sScriptDir & "\run.bat" & Chr(34), 0
Set WinScriptHost = Nothing