Example usage for java.lang ProcessBuilder directory

List of usage examples for java.lang ProcessBuilder directory

Introduction

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

Prototype

File directory

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

Click Source Link

Usage

From source file:com.googlecode.flyway.ant.AntLargeTest.java

/**
 * Runs Ant in this directory with these extra arguments.
 *
 * @param expectedReturnCode The expected return code for this invocation.
 * @param dir       The directory below src/test/resources to run maven in.
 * @param extraArgs The extra arguments (if any) for Maven.
 * @return The standard output.//from   w ww  . j  a v  a 2 s  . co m
 * @throws Exception When the execution failed.
 */
private String runAnt(int expectedReturnCode, String dir, String... extraArgs) throws Exception {
    String antHome = System.getenv("ANT_HOME");

    String extension = "";
    if (System.getProperty("os.name").startsWith("Windows")) {
        extension = ".bat";
    }

    List<String> args = new ArrayList<String>();
    args.add(antHome + "/bin/ant" + extension);
    args.add("-DlibDir=" + System.getProperty("libDir"));
    args.addAll(Arrays.asList(extraArgs));

    ProcessBuilder builder = new ProcessBuilder(args);
    builder.directory(new File(installDir + "/" + dir));
    builder.redirectErrorStream(true);

    Process process = builder.start();
    String stdOut = FileCopyUtils.copyToString(new InputStreamReader(process.getInputStream(), "UTF-8"));
    int returnCode = process.waitFor();

    System.out.print(stdOut);

    assertEquals("Unexpected return code", expectedReturnCode, returnCode);

    return stdOut;
}

From source file:net.sf.mavenjython.test.PythonTestMojo.java

public void execute() throws MojoExecutionException {
    // all we have to do is to run nose on the source directory
    List<String> l = new ArrayList<String>();
    if (program.equals("nose")) {
        l.add("nosetests.bat");
        l.add("--failure-detail");
        l.add("--verbose");
    } else {// ww w . j av a 2s  .  c  o m
        l.add(program);
    }

    ProcessBuilder pb = new ProcessBuilder(l);
    pb.directory(testOutputDirectory);
    pb.environment().put("JYTHONPATH", ".;" + outputDirectory.getAbsolutePath());
    final Process p;
    getLog().info("starting python tests");
    getLog().info("executing " + pb.command());
    getLog().info("in directory " + testOutputDirectory);
    getLog().info("and also including " + outputDirectory);
    try {
        p = pb.start();
    } catch (IOException e) {
        throw new MojoExecutionException(
                "Python tests execution failed. Provide the executable '" + program + "' in the path", e);
    }
    copyIO(p.getInputStream(), System.out);
    copyIO(p.getErrorStream(), System.err);
    copyIO(System.in, p.getOutputStream());
    try {
        if (p.waitFor() != 0) {
            throw new MojoExecutionException("Python tests failed with return code: " + p.exitValue());
        } else {
            getLog().info("Python tests (" + program + ") succeeded.");
        }
    } catch (InterruptedException e) {
        throw new MojoExecutionException("Python tests were interrupted", e);
    }
}

From source file:com.jbrisbin.vpc.jobsched.exe.ExeMessageHandler.java

public ExeMessage handleMessage(final ExeMessage msg) throws Exception {
    log.debug("handling message: " + msg.toString());

    List<String> args = msg.getArgs();
    args.add(0, msg.getExe());//  www  . j  a v a  2  s .c o  m

    try {
        ProcessBuilder pb = new ProcessBuilder(args);
        pb.environment().putAll(msg.getEnv());
        pb.directory(new File(msg.getDir()));
        pb.redirectErrorStream(true);
        Process p = pb.start();

        BufferedInputStream stdout = new BufferedInputStream(p.getInputStream());
        byte[] buff = new byte[4096];
        for (int bytesRead = 0; bytesRead > -1; bytesRead = stdout.read(buff)) {
            msg.getOut().write(buff, 0, bytesRead);
        }

        p.waitFor();
    } catch (Throwable t) {
        log.error(t.getMessage(), t);
        Object errmsg = t.getMessage();
        if (null != errmsg) {
            msg.getOut().write(((String) errmsg).getBytes());
        }
    }
    return msg;
}

From source file:com.blackducksoftware.integration.hub.detect.util.executable.Executable.java

public ProcessBuilder createProcessBuilder() {
    final List<String> processBuilderArguments = createProcessBuilderArguments();
    final ProcessBuilder processBuilder = new ProcessBuilder(processBuilderArguments);
    processBuilder.directory(workingDirectory);
    final Map<String, String> processBuilderEnvironment = processBuilder.environment();
    final Map<String, String> systemEnv = System.getenv();
    for (final String key : systemEnv.keySet()) {
        populateEnvironmentMap(processBuilderEnvironment, key, systemEnv.get(key));
    }// w w  w  .j ava 2 s .c  o  m
    for (final String key : environmentVariables.keySet()) {
        populateEnvironmentMap(processBuilderEnvironment, key, environmentVariables.get(key));
    }
    return processBuilder;
}

From source file:org.eclipse.smila.utils.scriptexecution.WindowsScriptExecutor.java

/**
 * Do execution of given command file./*from   w  w w. ja v  a 2s .co  m*/
 * 
 * @param file
 *          file
 * 
 * @return result code
 * 
 * @throws IOException
 *           IOException
 * @throws InterruptedException
 *           InterruptedException
 */
private int doExecute(final File file) throws IOException, InterruptedException {

    _log.debug("Do Execute " + file.getAbsolutePath());

    final ProcessBuilder processBuilder = new ProcessBuilder(file.getAbsolutePath());
    processBuilder.directory(file.getParentFile());
    processBuilder.redirectErrorStream(true);

    final Process process = processBuilder.start();

    final int resultCode = process.waitFor();

    LogHelper.debug(_log, process.getInputStream());

    return resultCode;
}

From source file:com.googlecode.flyway.commandline.largetest.CommandLineLargeTest.java

/**
 * Runs the Flyway Command Line tool.//from   ww  w  .  j a  va  2s .  c o  m
 *
 * @param expectedReturnCode The expected return code for this invocation.
 * @param configFileName     The config file name. {@code null} for default.
 * @param operation          The operation {@code null} for none.
 * @param extraArgs          The extra arguments to pass to the tool.
 * @return The standard output produced by the tool.
 * @throws Exception thrown when the invocation failed.
 */
private String runFlywayCommandLine(int expectedReturnCode, String configFileName, String operation,
        String... extraArgs) throws Exception {
    List<String> args = new ArrayList<String>();

    String installDir = System.getProperty("installDir");
    args.add(installDir + "/flyway." + flywayCmdLineExtensionForCurrentSystem());

    if (operation != null) {
        args.add(operation);
    }
    if (configFileName != null) {
        String configFile = new ClassPathResource("largeTest.properties").getFile().getPath();
        args.add("-configFile=" + configFile);
    }
    args.addAll(Arrays.asList(extraArgs));

    ProcessBuilder builder = new ProcessBuilder(args);
    builder.directory(new File(installDir));
    builder.redirectErrorStream(true);

    Process process = builder.start();
    String stdOut = FileCopyUtils.copyToString(new InputStreamReader(process.getInputStream(), "UTF-8"));
    int returnCode = process.waitFor();

    System.out.print(stdOut);

    assertEquals("Unexpected return code", expectedReturnCode, returnCode);

    return stdOut;
}

From source file:backtype.storm.utils.ShellProcess.java

public Number launch(Map conf, TopologyContext context) throws IOException {
    ProcessBuilder builder = new ProcessBuilder(command);
    builder.directory(new File(context.getCodeDir()));
    _subprocess = builder.start();//from w  ww.  ja  va 2  s .  co  m

    processIn = new DataOutputStream(_subprocess.getOutputStream());
    processOut = new BufferedReader(new InputStreamReader(_subprocess.getInputStream()));
    processErrorStream = _subprocess.getErrorStream();

    JSONObject setupInfo = new JSONObject();
    setupInfo.put("pidDir", context.getPIDDir());
    setupInfo.put("conf", conf);
    setupInfo.put("context", context);
    writeMessage(setupInfo);

    return (Number) readMessage().get("pid");
}

From source file:com.synopsys.integration.executable.ProcessBuilderRunner.java

public ProcessBuilder createProcessBuilder(Executable executable) {
    final ProcessBuilder processBuilder = new ProcessBuilder(executable.getCommandWithArguments());
    processBuilder.directory(executable.getWorkingDirectory());
    final Map<String, String> processBuilderEnvironment = processBuilder.environment();
    for (final String key : executable.getEnvironmentVariables().keySet()) {
        populateEnvironmentMap(processBuilderEnvironment, key, executable.getEnvironmentVariables().get(key));
    }//from ww w  .  java  2 s.c  o  m
    return processBuilder;
}

From source file:com.netflix.raigad.defaultimpl.ElasticSearchProcessManager.java

public void stop() throws IOException {
    logger.info("Stopping Elasticsearch server ....");
    List<String> command = Lists.newArrayList();
    if (!"root".equals(System.getProperty("user.name"))) {
        command.add(SUDO_STRING);//  w w  w  .j  a  v a 2s.  c o  m
        command.add("-n");
        command.add("-E");
    }
    for (String param : config.getElasticsearchStopScript().split(" ")) {
        if (StringUtils.isNotBlank(param))
            command.add(param);
    }
    ProcessBuilder stopCass = new ProcessBuilder(command);
    stopCass.directory(new File("/"));
    stopCass.redirectErrorStream(true);
    Process stopper = stopCass.start();

    sleeper.sleepQuietly(SCRIPT_EXECUTE_WAIT_TIME_MS);
    try {
        int code = stopper.exitValue();
        if (code == 0)
            logger.info("Elasticsearch server has been stopped");
        else {
            logger.error("Unable to stop Elasticsearch server. Error code: {}", code);
            logProcessOutput(stopper);
        }
    } catch (Exception e) {
        logger.warn("couldn't shut down Elasticsearch correctly", e);
    }
}

From source file:npm.modules.java

public void excutecmdwindow(String[] command, String workfolder) throws Exception {
    ProcessBuilder builder = new ProcessBuilder(command);
    builder.directory(new File(workfolder).getAbsoluteFile());
    builder.redirectErrorStream(true);//from  ww w.j  ava  2s. c  o  m
    builder.start();
}