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:edu.rice.dca.soaplabPBS.PBSJob.java

/**************************************************************************
 * Create and return a ProcessBuilder filled with the command-line
 * arguments and environment as defined in the metadata and user
 * input data for this job./*from w ww  .  j a  v a 2s  .c  o m*/
 *
 * It throws an exception when it was not able to fill the
 * ProcessBuilder.
 **************************************************************************/
protected ProcessBuilder getProcessBuilder() throws SoaplabException {

    // command-line arguments
    ProcessBuilder pb = new ProcessBuilder(createArgs());
    pb.command().add(0, getExecutableName());

    // environment from several sources...
    Map<String, String> env = pb.environment();
    // ...from the user (as defined by the service metadata)
    addProperties(env, createEnvs());
    // ...from the service configuration
    addProperties(env, Config.getMatchingProperties(Config.PROP_ENVAR, getServiceName(), this));
    // ...combine the current PATH and Soaplab's addtopath properties
    addToPath(env, Config.getStrings(Config.PROP_ADDTOPATH_DIR, null, getServiceName(), this));

    // working directory
    pb.directory(getJobDir());

    return pb;
}

From source file:hoot.services.command.CommandRunner.java

public CommandResult exec(String[] pCmd, Map<String, String> pEnv, boolean useSysEnv, File dir, Writer pOut,
        Writer pErr) throws IOException, InterruptedException {

    int out = 0;/*w  w w  .  jav  a2s  . c  om*/
    String pCmdString = ArrayUtils.toString(pCmd);
    ProcessBuilder builder = new ProcessBuilder();
    builder.command(pCmd);
    Map<String, String> env = builder.environment();
    if (!useSysEnv)
        env.clear();
    for (String name : pEnv.keySet()) {
        env.put(name, pEnv.get(name));
    }
    builder.directory(dir);

    logExec(pCmdString, env);

    StopWatch clock = new StopWatch();
    clock.start();
    try {
        process = builder.start();
        out = handleProcess(process, pCmdString, pOut, pErr, _outputList, sig_interrupt);
    } finally {
        this.cleanUpProcess();
        clock.stop();
        if (_log.isInfoEnabled())
            _log.info("'" + pCmdString + "' completed in " + clock.getTime() + " ms");
    }
    if (sig_interrupt.getValue() == true) {
        out = -9999;
    }
    CommandResult result = new CommandResult(pCmdString, out, pOut.toString(), pErr.toString());
    return result;
}

From source file:hoot.services.command.CommandRunner.java

public CommandResult exec(String[] pCmd, Map<String, String> pEnv, boolean useSysEnv, Writer pOut, Writer pErr)
        throws IOException, InterruptedException {

    int out = 0;//from  w  ww . java 2s.  c  o  m
    String pCmdString = ArrayUtils.toString(pCmd);
    ProcessBuilder builder = new ProcessBuilder();
    builder.command(pCmd);

    Map<String, String> env = builder.environment();
    if (!useSysEnv)
        env.clear();
    for (String name : pEnv.keySet()) {
        env.put(name, pEnv.get(name));
    }

    logExec(pCmdString, env);

    StopWatch clock = new StopWatch();
    clock.start();
    try {
        process = builder.start();
        out = handleProcess(process, pCmdString, pOut, pErr, _outputList, sig_interrupt);
    } finally {
        this.cleanUpProcess();
        clock.stop();
        if (_log.isInfoEnabled())
            _log.info("'" + pCmdString + "' completed in " + clock.getTime() + " ms");
    }

    if (sig_interrupt.getValue() == true) {
        out = -9999;
    }
    CommandResult result = new CommandResult(pCmdString, out, pOut.toString(), pErr.toString());
    return result;
}

From source file:org.atricore.josso.tooling.wrapper.InstallCommand.java

private int chmod(File serviceFile, String mode) throws Exception {
    ProcessBuilder builder = new ProcessBuilder();
    builder.command("chmod", mode, serviceFile.getCanonicalPath());
    Process p = builder.start();//from   w  w w.  j  av  a2s.c om

    PumpStreamHandler handler = new PumpStreamHandler(System.in, System.out, System.err);
    handler.attach(p);
    handler.start();
    int status = p.waitFor();
    handler.stop();
    return status;
}

From source file:org.apache.taverna.commandline.TavernaCommandLineTest.java

private void fetchTaverna(String location, String name) throws Exception {
    File zipFile = new File(buildDirectory, name + ".zip");
    IOUtils.copy(new URL(location).openStream(), new FileOutputStream(zipFile));
    ProcessBuilder processBuilder = new ProcessBuilder("unzip", "-q", name);
    processBuilder.redirectErrorStream(true);
    processBuilder.directory(buildDirectory);
    System.out.println(processBuilder.command());
    Process process = processBuilder.start();
    waitFor(process);/* w ww.  jav  a2s.c  o m*/
}

From source file:net.sf.mavenjython.JythonMojo.java

public void runJythonScriptOnInstall(File outputDirectory, List<String> args) throws MojoExecutionException {
    getLog().info("running " + args + " in " + outputDirectory);
    ProcessBuilder pb = new ProcessBuilder(args);
    pb.directory(outputDirectory);/*from w w w.j av a2 s.  co  m*/
    final Process p;
    try {
        p = pb.start();
    } catch (IOException e) {
        throw new MojoExecutionException("Executing jython failed. tried to run: " + pb.command(), e);
    }
    copyIO(p.getInputStream(), System.out);
    copyIO(p.getErrorStream(), System.err);
    copyIO(System.in, p.getOutputStream());
    try {
        if (p.waitFor() != 0) {
            throw new MojoExecutionException("Jython failed with return code: " + p.exitValue());
        }
    } catch (InterruptedException e) {
        throw new MojoExecutionException("Python tests were interrupted", e);
    }

}

From source file:org.apache.slider.providers.agent.TestAgentClientProvider2.java

@Test
public void testRunCommand() throws Exception {
    AgentClientProvider provider = new AgentClientProvider(null);
    File appPkgDir = new File("/tmp/pkg");
    File agentPkgDir = new File("/tmp/agt");
    File cmdDir = new File("/tmp/cmd");
    String client_script = "scripts/abc.py";

    List<String> commands = Arrays.asList("python", "-S",
            new File("/tmp/pkg").getAbsolutePath() + File.separator + "package" + File.separator
                    + "scripts/abc.py",
            "INSTALL", new File("/tmp/cmd").getAbsolutePath() + File.separator + "command.json",
            new File("/tmp/pkg").getAbsolutePath() + File.separator + "package",
            new File("/tmp/cmd").getAbsolutePath() + File.separator + "command-out.json", "DEBUG");

    ProcessBuilder pbMock = PowerMock.createMock(ProcessBuilder.class);
    Process procMock = PowerMock.createMock(Process.class);
    PowerMock.expectNew(ProcessBuilder.class, commands).andReturn(pbMock);

    expect(pbMock.environment()).andReturn(new HashMap<String, String>()).anyTimes();
    expect(pbMock.start()).andReturn(procMock);
    expect(pbMock.command()).andReturn(new ArrayList<String>());
    expect(procMock.waitFor()).andReturn(0);
    expect(procMock.exitValue()).andReturn(0);
    expect(procMock.getErrorStream()).andReturn(IOUtils.toInputStream("stderr", "UTF-8"));
    expect(procMock.getInputStream()).andReturn(IOUtils.toInputStream("stdout", "UTF-8"));

    PowerMock.replayAll();// ww  w  .  j  ava 2 s  . c  o m

    provider.runCommand(appPkgDir, agentPkgDir, cmdDir, client_script);
    PowerMock.verifyAll();
}

From source file:jenkins.plugins.asqatasun.AsqatasunRunner.java

public void callService() throws IOException, InterruptedException {
    File logFile = AsqatasunRunnerBuilder.createTempFile(contextDir, "log-" + new Random().nextInt() + ".log",
            "");//  www  .  ja  va 2  s.c  om
    File scenarioFile = AsqatasunRunnerBuilder.createTempFile(contextDir, scenarioName + "_#" + buildNumber,
            AsqatasunRunnerBuilder.forceVersion1ToScenario(scenario));

    ProcessBuilder pb = new ProcessBuilder(tgScriptName, "-f", firefoxPath, "-r", referential, "-l", level,
            "-d", displayPort, "-x", xmxValue, "-o", logFile.getAbsolutePath(), "-t", "Scenario",
            scenarioFile.getAbsolutePath());

    pb.directory(contextDir);
    pb.redirectErrorStream(true);
    listener.getLogger().print("Launching asqatasun runner with the following options : ");
    listener.getLogger().print(pb.command());
    Process p = pb.start();
    p.waitFor();

    extractDataAndPrintOut(logFile, listener.getLogger());

    if (!isDebug) {
        FileUtils.forceDelete(logFile);
    }

    FileUtils.forceDelete(scenarioFile);
}

From source file:net.doubledoordev.cmd.CurseModpackDownloader.java

private Process runForgeInstaller(File file) throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder("java", "-jar", file.getName(), "--installServer");
    StringBuilder joiner = new StringBuilder("Running forge installer with command: ");
    for (String cmd : processBuilder.command())
        joiner.append(cmd).append(' ');
    if (!quiet)/*from w  w  w . j a  va2s.  c om*/
        logger.println(joiner.toString());
    processBuilder.directory(output);
    processBuilder.redirectErrorStream(true);
    return processBuilder.start();
}

From source file:org.apache.taverna.commandline.TavernaCommandLineTest.java

private synchronized void runWorkflow(String command, String workflow, File outputsDirectory,
        boolean inputValues, boolean secure, boolean database) throws Exception {
    ProcessBuilder processBuilder = new ProcessBuilder("sh", command);
    processBuilder.redirectErrorStream(true);
    processBuilder.directory(buildDirectory);
    List<String> args = processBuilder.command();
    for (File input : inputs) {
        if (inputValues) {
            args.add("-inputvalue");
            args.add(input.getName());//from   w  w  w .  j ava2  s  . c  om
            args.add(IOUtils.toString(new FileReader(input)));
        } else {
            args.add("-inputfile");
            args.add(input.getName());
            args.add(input.getAbsolutePath());
        }
    }
    args.add("-outputdir");
    args.add(outputsDirectory.getPath());
    if (secure) {
        args.add("-cmdir");
        args.add(getClass().getResource("/security").getFile());
        args.add("-cmpassword");
    }
    if (database) {
        args.add("-embedded");
    }
    args.add(workflow);
    Process process = processBuilder.start();
    if (secure) {
        PrintStream outputStream = new PrintStream(process.getOutputStream());
        outputStream.println("test");
        outputStream.flush();
    }
    waitFor(process);
}