Java Utililty Methods exec

List of utility methods to do exec

Description

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

Method

ProcessexeCmdByOs(String cmd)
exe Cmd By Os
Process process = null;
Runtime runtime = Runtime.getRuntime();
String osName = System.getProperty("os.name");
if (osName.toLowerCase().startsWith("win")) {
    process = runtime.exec((new String[] { "cmd", "/C", cmd }));
} else {
    process = runtime.exec((new String[] { "sh", "-c", cmd }));
return process;
StringexecName()
exec Name
return isWindows() ? "rake.bat" : "rake";
intexecPrint(final boolean out, final String... command)
Executes the command, optionally copying output to standard output.
final ProcessBuilder builder = new ProcessBuilder(command);
builder.redirectErrorStream(true);
final Process p = builder.start();
final InputStream is = p.getInputStream();
int c;
while ((c = is.read()) >= 0)
    if (out) {
        System.out.write(c);
...
booleanexecProcess(String process)
exec Process
try {
    String command = "cmd /C start /wait " + process;
    Process p = Runtime.getRuntime().exec(command);
    p.waitFor();
} catch (IOException | InterruptedException e) {
    logError(e);
    return false;
return true;
intexecProcess(String[] cmdline, final long timeout)
execProcess
Process process = Runtime.getRuntime().exec(cmdline);
final Thread thread = Thread.currentThread();
Thread waitTimeout = new Thread() {
    public void run() {
        try {
            Thread.sleep(timeout);
            thread.interrupt();
        } catch (InterruptedException ignore) {
...
StringExecption2Strings(boolean rep, Throwable... execptions)
Execption Strings
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
for (Throwable e : execptions) {
    e.printStackTrace(pw);
if (rep) {
    return sw.toString().replaceAll("\r\n|\n\r|\r|\n", "<br/>\n");
return sw.toString();
ProcessexecScript(File shellScriptFile, String[] scriptCommand, PrintWriter execLog)
execScript is a function that executes shell scripts
Process child2 = Runtime.getRuntime().exec(scriptCommand);
String line;
BufferedReader in = new BufferedReader(new InputStreamReader(child2.getInputStream()));
while ((line = in.readLine()) != null) {
    execLog.println(line);
    execLog.flush();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(child2.getErrorStream()));
...
ProcessexecShell(String cmd)
exec Shell
return Runtime.getRuntime().exec(getShellArgs(cmd));
StringexecShell(String shell)
exec Shell
Runtime rt = Runtime.getRuntime();
Process process = rt.exec(shell);
return getOutputMsg(process);
voidexecSudoCommand(final String _sudoCmd, final String _pw)
exec Sudo Command
String[] cmd = { "/bin/bash", "-c", "echo " + _pw + "| sudo -S " + _sudoCmd };
try {
    Runtime.getRuntime().exec(cmd);
} catch (IOException e) {
    e.printStackTrace();