Java Utililty Methods Shell Command

List of utility methods to do Shell Command

Description

The list of methods to do Shell Command are organized into topic(s).

Method

intrunCommand(String[] args)
run Command
Runtime run = Runtime.getRuntime();
Process proc = null;
proc = run.exec(args);
BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
BufferedReader stderr = new BufferedReader(new InputStreamReader(proc.getErrorStream()));
while (true) {
    try {
        if (stdout.ready()) {
...
intrunCommand(String[] args)
run Command
return runCommand(args, null, null, false);
ProcessrunCommand(String[] cmdArray, String extraPath, String libPath)
run Command
ProcessBuilder processBuilder = new ProcessBuilder(cmdArray);
Map<String, String> environment = processBuilder.environment();
if (extraPath != null) {
    if (extraPath.isEmpty())
        extraPath = ".";
    environment.put("PATH", extraPath + ":" + environment.get("PATH"));
if (libPath != null) {
...
StringrunCommand(String[] command)
A new, fancy way of running external command.
String result = "";
try {
    ProcessBuilder builder = new ProcessBuilder(command);
    builder.redirectErrorStream(true);
    Process process = builder.start();
    String line;
    BufferedReader bri = new BufferedReader(new InputStreamReader(process.getInputStream()));
    while ((line = bri.readLine()) != null) {
...
StringrunCommand(String[] command)
run Command
Process process;
try {
    process = executeCommand(command);
    process.waitFor();
    String output = toString(process.getInputStream());
    return output;
} catch (Exception e) {
    throw new Exception(
...
IntegerrunCommandAndWait(String command, String workingDir, String extraPath)
Run command in another process and wait for it to finish.
String path = System.getenv("PATH");
if (extraPath != null) {
    path = extraPath + System.getProperty("path.separator") + path;
Process proc = Runtime.getRuntime().exec(command,
        new String[] { "M2_HOME=" + MVN_LOCATION, "PATH=" + path }, new File(workingDir));
File buildlog = new File(workingDir + "/build.log");
BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
...
voidrunCommandArray(String[] command)
Run curl command
Process p = null;
try {
    p = Runtime.getRuntime().exec(command);
} catch (IOException e1) {
    e1.printStackTrace();
} catch (Exception e1) {
    e1.printStackTrace();
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = null;
try {
    while ((line = input.readLine()) != null)
        System.out.println(line);
} catch (IOException e) {
    e.printStackTrace();
try {
    p.waitFor();
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (Exception e1) {
    e1.printStackTrace();
ListrunCommandGetOutput(String startFolder, String[] args, int[] cmdReturnValue)
Execute process and collect all output.
List<String> commands = new ArrayList<String>();
commands.addAll(Arrays.asList(args));
cmdReturnValue[0] = -1;
if (startFolder == null) {
    startFolder = System.getProperty("user.home");
ProcessBuilder pb = new ProcessBuilder(commands);
pb.directory(new File(startFolder));
...
voidrunCommandPrompt(final String commandLine, final IPath path)
This executes the command line for running the command prompt.
final String parsedCommandLine = parse(commandLine, path, false);
Runtime.getRuntime().exec(parsedCommandLine, null, getContainingFolder(path).toFile());
voidrunCommandWithOutput(String cmd)
run Command With Output
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
BufferedReader br = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((br.readLine()) != null) {
    System.out.println(line);