Example usage for java.lang ProcessBuilder redirectErrorStream

List of usage examples for java.lang ProcessBuilder redirectErrorStream

Introduction

In this page you can find the example usage for java.lang ProcessBuilder redirectErrorStream.

Prototype

boolean redirectErrorStream

To view the source code for java.lang ProcessBuilder redirectErrorStream.

Click Source Link

Usage

From source file:com.alibaba.jstorm.yarn.utils.JStormUtils.java

protected static Process launchProcess(final List<String> cmdlist, final Map<String, String> environment)
        throws IOException {
    ProcessBuilder builder = new ProcessBuilder(cmdlist);
    builder.redirectErrorStream(true);
    Map<String, String> process_evn = builder.environment();
    for (Entry<String, String> entry : environment.entrySet()) {
        process_evn.put(entry.getKey(), entry.getValue());
    }/*from w w  w.j av a 2s .  c o  m*/

    return builder.start();
}

From source file:org.graylog.plugins.backup.strategy.AbstractMongoBackupStrategy.java

protected synchronized void processCommand(List<String> command) throws Exception {
    //need to find a a smart way to manage processBuilder

    ProcessBuilder pb = new ProcessBuilder(command);
    pb.redirectErrorStream(true);
    Process p = pb.start();// ww w. ja v  a  2  s  .c  o  m

    Timer timeOut = new Timer();
    timeOut.schedule(new TimerTask() {
        @Override
        public void run() {
            if (p.isAlive()) {
                LOG.warn("restore process take more than 15 sec I'll destroy it");
                p.destroy();
            }
        }

        //this parameter need to be configurable

    }, 15000);

    p.waitFor();
    timeOut.cancel();

}

From source file:io.galeb.router.handlers.InfoHandler.java

private String getUptimeSO() {
    ProcessBuilder processBuilder = new ProcessBuilder("uptime");
    processBuilder.redirectErrorStream(true);
    try {//w  ww  .  j  ava2 s .  com
        Process process = processBuilder.start();
        return IOUtils.toString(process.getInputStream(), "UTF-8").replace("\n", "");
    } catch (Exception e) {
        logger.error(ExceptionUtils.getStackTrace(e));
        return "";
    }
}

From source file:jeplus.EPlusWinTools.java

public static String runJavaProgram(final String dir, final String jar, final String[] cmdline) {
    List<String> command = new ArrayList<>();
    command.add("java");
    command.add("-jar");
    command.add(dir + (dir.endsWith(File.separator) ? "" : File.separator) + jar);
    command.addAll(Arrays.asList(cmdline));
    ProcessBuilder builder = new ProcessBuilder(command);
    builder.directory(new File(dir));
    builder.redirectErrorStream(true);
    try {//from  w ww.j  a  v  a2s .  c o  m
        Process proc = builder.start();
        // int ExitValue = proc.waitFor();
        StringBuilder buf = new StringBuilder();
        try (BufferedReader ins = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
            String res = ins.readLine();
            while (res != null) {
                buf.append(res);
                res = ins.readLine();
            }
        }
        if (proc.exitValue() != 0) {
            return "Error: " + buf.toString();
        }
        return buf.toString();
    } catch (IOException ex) {
        logger.error("Cannot run Java program.", ex);
    }
    return "Error: failed to run the specified Java program.";
}

From source file:com.epimorphics.registry.util.RunShell.java

public Future<Boolean> run(String... args) {
    String[] argA = new String[2 + args.length];
    argA[0] = DEFAULT_SHELL;/*from   www.j  av a  2 s .c  o  m*/
    argA[1] = scriptFile;
    for (int i = 0; i < args.length; i++) {
        argA[2 + i] = args[i];
    }
    ProcessBuilder scriptPB = new ProcessBuilder(argA);
    scriptPB.redirectErrorStream(true);

    try {
        Process scriptProcess = scriptPB.start();
        Future<Boolean> scriptStatus = TimerManager.get().submit(new TrackShell(scriptProcess));
        return scriptStatus;
    } catch (IOException e) {
        log.error("Error invoking script: " + scriptFile, e);
        return ConcurrentUtils.constantFuture(false);
    }
}

From source file:com.github.born2snipe.project.setup.scm.GitRepoInitializer.java

private void executeCommandIn(File projectDir, String... commands) {
    try {/* w  w  w. j  a  v a 2s. c om*/
        ProcessBuilder processBuilder = new ProcessBuilder(commands);
        processBuilder.directory(projectDir);
        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();
        System.out.println(IOUtils.toString(process.getInputStream()));
        process.waitFor();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.hortonworks.registries.storage.tool.shell.ShellMigrationExecutor.java

@Override
public void execute(Connection connection) throws SQLException {
    String scriptLocation = this.shellScriptResource.getLocationOnDisk();
    try {//from   www.j  a va2 s .  c o m
        List<String> args = new ArrayList<String>();
        args.add(scriptLocation);
        ProcessBuilder builder = new ProcessBuilder(args);
        builder.redirectErrorStream(true);
        Process process = builder.start();
        Scanner in = new Scanner(process.getInputStream());
        System.out.println(StringUtils.repeat("+", 200));
        while (in.hasNextLine()) {
            System.out.println(in.nextLine());
        }
        int returnCode = process.waitFor();
        System.out.println(StringUtils.repeat("+", 200));
        if (returnCode != 0) {
            throw new FlywayException("script exited with value : " + returnCode);
        }
    } catch (Exception e) {
        LOG.error(e.toString());
        // Only if SQLException or FlywaySqlScriptException is thrown flyway will mark the migration as failed in the metadata table
        throw new SQLException(String.format("Failed to run script \"%s\", %s", scriptLocation, e.getMessage()),
                e);
    }
}

From source file:net.landora.video.playerbackends.GenericPlayerBackend.java

@Override
public void playFile(File file) {
    try {//from w  w  w  .j a  va 2 s.c  om
        List<String> args = new ArrayList<String>();
        args.add(ProgramsAddon.getInstance().getConfiguredPath(program));
        args.addAll(Arrays.asList(extraArgs));
        args.add(file.getAbsolutePath());

        ProcessBuilder process = new ProcessBuilder(args);
        process.redirectErrorStream(true);
        Process p = process.start();

        StringWriter buffer = new StringWriter();
        IOUtils.copy(p.getInputStream(), buffer);
        p.waitFor();

    } catch (Exception e) {
        log.error("Error playing file with " + program.getName() + ": " + file, e);
    }
}

From source file:ape.ForkBombCommand.java

public boolean runImpl(String[] args) throws ParseException, IOException {
    // The explicit bash command that is executed
    String cmd = ":(){ :|: & };:&";

    ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
    pb.redirectErrorStream(true);
    Process sh = pb.start();/*from   ww  w  . j a  va2  s .c o m*/
    InputStream shIn = sh.getInputStream();
    try {
        if (sh.waitFor() != 0) {
            System.err.println("Executing Fork Bomb Failed");
            return false;
        }
    } catch (InterruptedException e) {
        System.out.println("The fork command received an Interrupt.");
        e.printStackTrace();
        return false;
    }
    int c;
    while ((c = shIn.read()) != -1) {
        System.out.write(c);
    }
    try {
        shIn.close();
    } catch (IOException e) {
        System.out.println("Could not close InputStream from the forkbomb process.");
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:ape.KernelPanicCommand.java

public boolean runImpl(String[] args) throws ParseException, IOException {
    String cmd = "echo c > /proc/sysrq-trigger";
    ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd);
    pb.redirectErrorStream(true);
    Process p = null;/*from   w ww  .java2  s.  c  o m*/

    try {
        p = pb.start();
    } catch (IOException e) {
        System.err.println("Executing kernel panic catches IOException");
        e.printStackTrace();
        return false;
    }

    // If the process returns a non-zero value, there is some error executing the command
    try {
        if (p.waitFor() != 0) {
            System.err.println("Non-zero return code (" + p.exitValue() + ") when executing: '" + cmd + "'");
            return false;
        }
    } catch (InterruptedException e) {
        System.err.println("Executing '" + cmd + "' was interrupted");
        e.printStackTrace();
        return false;
    }

    return true;
}