Java exec executeIt(String command)

Here you can find the source of executeIt(String command)

Description

Execute the given system command and return a Map containing output results and exit value.

License

Apache License

Parameter

Parameter Description
command a parameter

Return

Map containing output results and exit value

Declaration

public static Map<String, String> executeIt(String command) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Main {
    /**/*w ww . j ava 2  s  .  c o  m*/
     * Execute the given system command and return a Map containing output
     * results and exit value.
     *
     * @param command
     * @return Map containing output results and exit value
     */
    public static Map<String, String> executeIt(String command) {
        StringBuilder output = new StringBuilder();
        Process process = null;
        try {
            process = Runtime.getRuntime().exec(command);
            Scanner sc = new Scanner(process.getInputStream());
            process.waitFor();
            while (sc.hasNext()) {
                output.append(sc.nextLine());
            }
        } catch (IOException e) {
            output.append(e.getMessage());
        } catch (InterruptedException e) {
            output.append(e.getMessage());
        }
        Map<String, String> result = new HashMap<String, String>();
        result.put("exitValue", "" + process.exitValue());
        result.put("out", output.toString());
        return result;
    }
}

Related

  1. executeCommandLinux(final String _command)
  2. executeDELETE(String parameters)
  3. executeDotCommand(final File dotFile)
  4. executeGET(DataInputStream sockInp, DataOutputStream sockOutp)
  5. executeGetStatus(ProcessBuilder pb)
  6. executeLocalCommand(String[] command)
  7. executeLS()
  8. executeMemoryInfoProcess(String... command)
  9. executeNativeCommand(String command)