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

StringgetJavaVersion(String command)
Retrieves the version of the java vm indicated by the command string.
String run_command = "'" + command + "' -version";
System.out.println(run_command);
Process p;
String[] args = { command, "-version" };
p = Runtime.getRuntime().exec(args);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = reader.readLine();
...
intrunCommand(final ProcessBuilder command)
run Command
final Process process = command.start();
new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            final BufferedReader reader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            try {
...
StringrunCommand(String cmd)
Run the specified command and return the output as a string.
try {
    Process p = Runtime.getRuntime().exec(cmd);
    BufferedReader cin = new BufferedReader(new InputStreamReader(p.getInputStream()));
    StringBuilder buffer = new StringBuilder();
    String line = "";
    while (line != null) {
        buffer.append(line);
        line = cin.readLine();
...
voidrunCommand(String cmd)
Calls runCommand/2 assuming that wait=true.
runCommand(cmd, true);
InputStreamrunCommand(String command)
run Command
System.out.println(command);
Process p = Runtime.getRuntime().exec(new String[] { "bash", "-c", command });
InputStream inputStream = p.getInputStream();
return inputStream;
StringrunCommand(String command)
run Command
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line = "";
StringBuilder sb = new StringBuilder();
while ((line = reader.readLine()) != null)
    sb.append(line + "\n");
return sb.toString();
...
booleanrunCommand(String command, String args, String file)
run Command
logOut("Trying to exec:\n   cmd = " + command + "\n   args = " + args + "\n   %s = " + file);
String[] parts = prepareCommand(command, args, file);
try {
    Process p = Runtime.getRuntime().exec(parts);
    if (p == null)
        return false;
    try {
        int retval = p.exitValue();
...
voidrunCommand(String command[])
run Command
new ProcessBuilder(command).start();
StringrunCommand(String program, ArrayList args)
Helper function to run a system command and return the stdout / stderr as a string
ArrayList<String> command = new ArrayList<String>();
command.add(program);
if (args != null) {
    for (String arg : args) {
        command.add(arg);
System.out.println("RUNNING COMMAND :" + join(command, " "));
...
voidrunCommand(String s)
run Command
try {
    Runtime.getRuntime().exec(s);
} catch (IOException e) {
    e.printStackTrace();