Example usage for java.lang ProcessBuilder command

List of usage examples for java.lang ProcessBuilder command

Introduction

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

Prototype

List command

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

Click Source Link

Usage

From source file:net.sourceforge.pmd.it.PMDExecutor.java

private static ExecutionResult runPMDWindows(Path tempDir, String... arguments) throws Exception {
    String cmd = tempDir.resolve(PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/pmd.bat").toAbsolutePath()
            .toString();/*from ww  w  .j  a  v a2 s .  c o  m*/
    ProcessBuilder pb = new ProcessBuilder(cmd);
    pb.command().addAll(Arrays.asList(arguments));
    pb.redirectErrorStream(true);
    Process process = pb.start();
    String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);

    int result = process.waitFor();
    return new ExecutionResult(result, output);
}

From source file:net.sourceforge.pmd.it.PMDExecutor.java

private static ExecutionResult runPMDUnix(Path tempDir, String... arguments) throws Exception {
    String cmd = tempDir.resolve(PMD_BIN_PREFIX + PMDVersion.VERSION + "/bin/run.sh").toAbsolutePath()
            .toString();/*from  w w w .ja v  a  2 s.com*/
    ProcessBuilder pb = new ProcessBuilder(cmd, "pmd");
    pb.command().addAll(Arrays.asList(arguments));
    pb.redirectErrorStream(true);
    Process process = pb.start();
    String output = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);

    int result = process.waitFor();
    return new ExecutionResult(result, output);
}

From source file:kr.ac.kaist.wala.hybridroid.analysis.resource.AndroidDecompiler.java

private static void permission(String path) {
    String[] cmd = { "chmod", "755", path };
    ProcessBuilder pb = new ProcessBuilder();
    pb.command(cmd);

    Process p = null;/*  w w  w. jav a  2  s  .  co  m*/
    try {
        p = pb.start();
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        BufferedReader bre = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        String r = null;

        while ((r = br.readLine()) != null) {
            System.out.println(r);
        }

        while ((r = bre.readLine()) != null) {
            System.err.println(r);
        }

        int res = p.waitFor();
        if (res != 0) {
            throw new InternalError("failed to decompile: " + path);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.junoyoon.BullsUtil.java

/**
 * Run command and//from w  w w .ja v  a 2  s  .co m
 * 
 * @param cmd
 * @return
 * @throws IOException
 */
public static String getCmdOutput(List<String> cmd) throws IOException {
    ProcessBuilder builder = new ProcessBuilder();
    builder.command().addAll(cmd);
    builder.redirectErrorStream(true);
    StringBuilder result = new StringBuilder(1024);
    Process proc = builder.start();
    boolean firstLine = true;
    for (Object eachLineObject : IOUtils.readLines(proc.getInputStream())) {
        String eachLine = (String) eachLineObject;
        if (firstLine) {
            eachLine = eachLine.replace("charset=us-ascii", "charset=" + Constant.DEFAULT_ENCODING);
            firstLine = false;
        }
        result.append(eachLine).append("\n");
    }
    return result.toString();
}

From source file:end2endtests.runner.ProcessRunner.java

private static Process startProcess(File workingDir, List<String> command) throws IOException {
    ProcessBuilder builder = new ProcessBuilder();
    builder.directory(workingDir);/*from  w  ww.j  av  a  2s  . c om*/
    builder.redirectErrorStream(false);
    builder.command(command);
    return builder.start();
}

From source file:io.github.retz.executor.FileManager.java

private static void fetchHDFSFile(String file, String dest) throws IOException {
    LOG.debug("Downloading {} to {} as HDFS file", file, dest);
    // TODO: make 'hadoop' command arbitrarily specifiable, but given that mesos-agent (slave) can fetch hdfs:// files, it should be also available, too
    String[] hadoopCmd = { "hadoop", "fs", "-copyToLocal", file, dest };
    LOG.debug("Command: {}", String.join(" ", hadoopCmd));
    ProcessBuilder pb = new ProcessBuilder();
    pb.command(hadoopCmd).inheritIO();

    Process p = pb.start();//  w w  w . j a va 2 s  .c  o m
    while (true) {
        try {
            int result = p.waitFor();
            if (result != 0) {
                LOG.error("Downloading {} failed: {}", file, result);
            } else {
                LOG.info("Download finished: {}", file);
            }
            return;
        } catch (InterruptedException e) {
            LOG.error("Download process interrupted: {}", e.getMessage()); // TODO: debug?
        }
    }
}

From source file:com.frederikam.fredboat.bootloader.Bootloader.java

private static Process boot() throws IOException {
    //Check that we are not booting too quick (we could be stuck in a login loop)
    if (System.currentTimeMillis() - lastBoot > 3000 * 1000) {
        recentBoots = 0;//from   w w w  .  j  a v  a  2s.c o  m
    }

    recentBoots++;
    lastBoot = System.currentTimeMillis();

    if (recentBoots >= 4) {
        System.out.println("[BOOTLOADER] Failed to restart 3 times, probably due to login errors. Exiting...");
        System.exit(-1);
    }

    //ProcessBuilder pb = new ProcessBuilder(System.getProperty("java.home") + "/bin/java -jar "+new File("FredBoat-1.0.jar").getAbsolutePath())
    ProcessBuilder pb = new ProcessBuilder().inheritIO();
    ArrayList<String> list = new ArrayList<>();
    command.forEach((Object str) -> {
        list.add((String) str);
    });

    pb.command(list);

    Process process = pb.start();
    return process;
}

From source file:Main.java

private static void openWindows(File f) {
    try {/* w  ww .j  a  v  a 2 s .c  o m*/
        ProcessBuilder builder = new ProcessBuilder();
        List<String> lst = new ArrayList<String>();
        lst.add("rundll32");
        lst.add("url.dll,FileProtocolHandler");
        lst.add(f.getAbsolutePath());
        builder.command(lst);
        builder.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:uk.dsxt.voting.tests.TestsLauncher.java

private static void startProcess(String name, String jarPath, String[] params) {
    if (processesByName.containsKey(name)) {
        stopProcess(name);/*w  w w.j  ava2s  .  c om*/
    }
    try {
        List<String> cmd = new ArrayList<>();
        cmd.add("java");
        cmd.add("-jar");
        cmd.add(jarPath);
        Collections.addAll(cmd, params);

        log.debug("Start process command: {}", StringUtils.join(cmd, " "));

        ProcessBuilder processBuilder = new ProcessBuilder();
        processBuilder.command(cmd);
        processBuilder.redirectError(new File(LOGS_FOLDER, String.format("error_%s.log", name)));
        processBuilder.redirectOutput(new File(LOGS_FOLDER, String.format("output_%s.log", name)));
        Process process = processBuilder.start();
        processesByName.put(name, process);
        log.info("Process {} started", name);
    } catch (Exception e) {
        log.error(String.format("Can't run process %s", name), e);
    }
}

From source file:functionaltests2.SchedulerCommandLine.java

/**
 * Start a Scheduler and Resource Manager.
 *//*from w ww.  j  a va2  s  . co m*/
public static void startSchedulerCmdLine(boolean restart, File proactiveConf) throws Exception {

    File schedHome = new File(System.getProperty("pa.scheduler.home")).getCanonicalFile();
    File rmHome = new File(System.getProperty("pa.rm.home")).getCanonicalFile();
    if (proactiveConf != null) {
        FileUtils.copyFile(proactiveConf,
                new File(schedHome, "config" + fs + "proactive" + fs + "ProActiveConfiguration.xml"));
    }

    System.out.println(schedHome);

    p = null;
    ProcessBuilder pb = new ProcessBuilder();
    if (OperatingSystem.getOperatingSystem().equals(OperatingSystem.unix)) {
        pb.directory(new File(schedHome + fs + "bin" + fs + "unix"));
        pb.command("/bin/bash", restart ? "scheduler-start" : "scheduler-start-clean",
                "-Dproactive.communication.protocol=pnp", "-Dproactive.pnp.port=9999");
        pb.environment().put("SchedulerTStarter", "SchedulerTStarter");
        p = pb.start();

    } else {

        pb.directory(new File(schedHome + fs + "bin" + fs + "windows"));

        pb.command("cmd.exe", "/c", restart ? "scheduler-start.bat" : "scheduler-start-clean.bat",
                "-Dproactive.communication.protocol=pnp", "-Dproactive.pnp.port=9999");
        pb.environment().put("SchedulerTStarter", "SchedulerTStarter");
        p = pb.start();

    }

    IOTools.LoggingThread lt1 = new IOTools.LoggingThread(p.getInputStream(), "[SchedulerTStarter]",
            System.out);
    Thread t1 = new Thread(lt1, "SchedulerTStarter");
    t1.setDaemon(true);
    t1.start();

    // waiting the initialization
    RMAuthentication rmAuth = RMConnection.waitAndJoin("pnp://localhost:9999");

    System.out.println("RM successfully joined.");

    SchedulerConnection.waitAndJoin("pnp://localhost:9999");
    System.out.println("Scheduler successfully joined.");

}