Example usage for java.lang ProcessBuilder start

List of usage examples for java.lang ProcessBuilder start

Introduction

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

Prototype

public Process start() throws IOException 

Source Link

Document

Starts a new process using the attributes of this process builder.

Usage

From source file:mamo.vanillaVotifier.ShellCommandSender.java

@NotNull
public Process sendCommand(@NotNull String command, @Nullable Map<String, String> environment)
        throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder(new StrTokenizer(command).getTokenArray());
    if (environment != null) {
        processBuilder.environment().putAll(environment);
    }/*from w w w. j a  v a2 s. c  o  m*/
    return processBuilder.start();
}

From source file:com.dickthedeployer.dick.web.service.CommandService.java

public String invoke(Path workingDir, String... command) throws RuntimeException {
    try {//ww w .  jav a2  s  . co m
        log.info("Executing command {} in path {}", Arrays.toString(command), workingDir.toString());
        StringBuilder text = new StringBuilder();
        ProcessBuilder builder = new ProcessBuilder(command);
        builder.directory(workingDir.toFile());
        builder.redirectErrorStream(true);

        Process process = builder.start();

        try (Scanner s = new Scanner(process.getInputStream())) {
            while (s.hasNextLine()) {
                text.append(s.nextLine());
            }
            int result = process.waitFor();
            log.info("Process exited with result {} and output {}", result, text);

            if (result != 0) {
                throw new CommandExecutionException();
            }
            return text.toString();
        }
    } catch (IOException | InterruptedException ex) {
        throw new CommandExecutionException(ex);
    }
}

From source file:fi.hsl.parkandride.front.ProtractorTest.java

@Test
public void protractor_tests() throws InterruptedException, IOException {
    File projectDir = new File(System.getProperty("user.dir"));
    if (projectDir.getName().equals("application")) {
        projectDir = projectDir.getParentFile();
    }/*from   w w  w.  j av  a 2  s .c  om*/
    File script = new File(projectDir, "etc/protractor/protractor.sh");
    ProcessBuilder builder = new ProcessBuilder("bash", script.getAbsolutePath(), "test",
            "--browser=" + browser, "--baseUrl=http://localhost:" + port).inheritIO();
    Process process = builder.start();
    if (process.waitFor() != 0) {
        Assert.fail("Protractor tests failed");
    }
}

From source file:de.jcup.egradle.core.process.SimpleProcessExecutor.java

Process startProcess(ProcessBuilder pb) throws IOException {
    return pb.start();
}

From source file:org.anarres.qemu.image.QEmuImage.java

/**
 * Creates this image.//from  w  ww  .  jav a  2  s  . c  o m
 *
 * @param format The image format for the new image.
 * @param size The virtual size of the new image.
 */
public void create(@Nonnull QEmuImageFormat format, @Nonnegative long size) throws IOException {
    ProcessBuilder builder = new ProcessBuilder("qemu-img", "create", "-f", format.name(),
            file.getAbsolutePath(), String.valueOf(size));
    Process process = builder.start();
    ByteStreams.copy(process.getInputStream(), System.err);
}

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);// w w w . j  a  v  a2  s. c  om
    Process p = null;

    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;
}

From source file:net.landora.video.programs.ProgramsAddon.java

public boolean isAvaliable(Program program) {
    String path = getConfiguredPath(program);
    if (path == null) {
        return false;
    }/*from w  w w  . j  a v  a 2 s .c o m*/

    ArrayList<String> command = new ArrayList<String>();
    command.add(path);
    command.addAll(program.getTestArguments());
    ProcessBuilder builder = new ProcessBuilder(command);
    builder.redirectErrorStream(true);

    try {
        Process p = builder.start();
        IOUtils.copy(p.getInputStream(), new NullOutputStream());
        p.waitFor();
        return true;
    } catch (Exception e) {
        log.info("Error checking for program: " + program, e);
        return false;
    }
}

From source file:ape.RemountFileSysReadOnlyCommand.java

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

    try {
        p = pb.start();
    } catch (IOException e) {
        System.err.println("Executing remount 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;
}

From source file:com.thoughtworks.go.agent.AgentProcessParentImpl.java

Process invoke(String[] command) throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    return processBuilder.start();
}

From source file:com.github.greengerong.NgProtractor.java

private void executeCommand(final String cmd, final String logAction) {
    final Log log = getLog();

    try {//from  ww  w . j  a va 2  s  .c  om
        log.info(String.format("execute %s running: %s", logAction, cmd));
        final ProcessBuilder processBuilder = new ProcessBuilder(getCommandAccordingToOS(cmd));
        final Process process = processBuilder.start();
        final String runningInfo = IOUtil.toString(process.getInputStream());
        log.info(runningInfo);
    } catch (IOException e) {
        log.warn(String.format("execute %s running script error", logAction), e);
    }
}