Below are 2 ways to run commands using the command line and get the result.
- Using ProcessBuilder
String[] commands = {"cmd.exe", "/c", "java -version"};
ProcessBuilder builder = new ProcessBuilder(commands);
//Redirect the process's standard error into standard output
builder.redirectErrorStream(true);
Process process = builder.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
log.info("Here is the output of the command:\n");
String s;
while ((s = stdInput.readLine()) != null) {
log.info(s);
}
2. Using Runtime.getRuntime().exec()
Runtime runtime = Runtime.getRuntime();
String[] commands = {"cmd.exe", "/c", "ping google.com"};
Process process = runtime.exec(commands);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// Read the output from the command
log.info("Here is the standard output of the command:\n");
String s;
while ((s = stdInput.readLine()) != null) {
log.info(s);
}
// Read any errors from the attempted command
log.info("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
log.info(s);
}
Full example code:
package eu.ibagroup.easyrpa.cmd;
import eu.ibagroup.easyrpa.engine.annotation.ApTaskEntry;
import eu.ibagroup.easyrpa.engine.apflow.ApTask;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
@ApTaskEntry(name = "Cmd Automation")
@Slf4j
public class CmdTask extends ApTask {
@Override
public void execute() {
try {
printJavaVersion();
pingGoogle();
} catch (Exception e) {
log.error("Error occurred", e);
}
}
private void printJavaVersion() throws IOException {
String[] commands = { "cmd.exe", "/c", "java -version" };
ProcessBuilder builder = new ProcessBuilder(commands);
// Redirect the process's standard error into standard output
builder.redirectErrorStream(true);
Process process = builder.start();
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
log.info("Here is the output of the command:\n");
String s;
while ((s = stdInput.readLine()) != null) {
log.info(s);
}
}
private void pingGoogle() throws IOException {
Runtime runtime = Runtime.getRuntime();
String[] commands = { "cmd.exe", "/c", "ping google.com" };
Process process = runtime.exec(commands);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
// Read the output from the command
log.info("Here is the standard output of the command:\n");
String s;
while ((s = stdInput.readLine()) != null) {
log.info(s);
}
// Read any errors from the attempted command
log.info("Here is the standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
log.info(s);
}
}
}
package eu.ibagroup.easyrpa.cmd;
import eu.ibagroup.easyrpa.engine.annotation.ApModuleEntry;
import eu.ibagroup.easyrpa.engine.apflow.ApModule;
import eu.ibagroup.easyrpa.engine.apflow.TaskOutput;
@ApModuleEntry(name = "KB - Cmd Automation Demo")
public class CmdDemoAp extends ApModule {
public TaskOutput run() throws Exception {
return execute(getInput(), CmdTask.class).get();
}
}
package eu.ibagroup.easyrpa.cmd;
import eu.ibagroup.easyrpa.engine.boot.ApModuleRunner;
public class CmdDemoApLocalRunner {
public static void main(String[] args) {
ApModuleRunner.localLaunch(CmdDemoAp.class);
}
}