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:org.testeditor.dsl.common.util.MavenExecutor.java

/**
 * Executes a maven build in a new jvm. The executable of the current jvm is
 * used to create a new jvm./*  w  w  w.  j av a 2s  .  co m*/
 * 
 * @param parameters
 *            for maven (separated by spaces, e.g. "clean integration-test"
 *            to execute the given goals)
 * @param pathtoPom
 *            path to the folder where the pom.xml is located.
 * @param testParam
 *            pvm parameter to identify the test case to be executed.
 * @param monitor
 *            Progress monitor to handle cancel events.
 * @return the result interpreted as {@link IStatus}.
 * @throws IOException
 *             on failure
 */
public int executeInNewJvm(String parameters, String pathToPom, String testParam, IProgressMonitor monitor,
        OutputStream outputStream) throws IOException {
    List<String> command = createMavenExecCommand(parameters, testParam);
    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.directory(new File(pathToPom));
    logger.info("Executing maven in folder='{}'.", pathToPom);
    logger.info("Executing maven in new jvm with command='{}'.", command);
    processBuilder.command(command);
    Process process = processBuilder.start();
    PrintStream out = new PrintStream(outputStream);
    OutputStreamCopyUtil outputCopyThread = new OutputStreamCopyUtil(process.getInputStream(), out);
    OutputStreamCopyUtil errorCopyThread = new OutputStreamCopyUtil(process.getErrorStream(), out);
    outputCopyThread.start();
    errorCopyThread.start();
    try {
        while (!process.waitFor(100, TimeUnit.MILLISECONDS)) {
            if (monitor.isCanceled()) {
                process.destroy();
                out.println("Operation cancelled.");
                return IStatus.CANCEL;
            }
        }
        return process.exitValue() == 0 ? IStatus.OK : IStatus.CANCEL;
    } catch (InterruptedException e) {
        logger.error("Caught exception.", e);
        return IStatus.ERROR;
    }
}

From source file:au.edu.unsw.cse.soc.federatedcloud.community.driven.cloudbase.connectors.docker.DockerConnector.java

@Override
public Result deploy(int resourceID) {
    String scriptFileLocation = "./tmp/" + resourceID + ".sh";
    //run the deployment script
    ProcessBuilder pb = new ProcessBuilder(scriptFileLocation);
    Map<String, String> env = pb.environment();
    pb.directory(new File("."));
    try {/* www .jav  a2 s.  c o m*/
        Process p = pb.start();

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        // read the output from the command
        System.out.println("Here is the standard output of the command:\n");
        String s = null;
        while ((s = stdInput.readLine()) != null) {
            log.info(s);
        }

        // read any errors from the attempted command
        System.out.println("Here is the standard error of the command (if any):\n");
        while ((s = stdError.readLine()) != null) {
            log.info(s);
        }
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }

    return null;
}

From source file:org.apache.servicecomb.it.deploy.NormalDeploy.java

protected ProcessBuilder createProcessBuilder(String[] cmds) {
    ProcessBuilder processBuilder = new ProcessBuilder(cmds).redirectErrorStream(true);
    if (deployDefinition.getWorkDir() != null) {
        processBuilder.directory(new File(deployDefinition.getWorkDir()));
    }/*w w w .  ja  va2  s .  c  o  m*/
    return processBuilder;
}

From source file:com.netflix.dynomitemanager.defaultimpl.FloridaProcessManager.java

public void stop() throws IOException {
    logger.info("Stopping Dynomite 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 . co m
        command.add("-n");
        command.add("-E");
    }
    for (String param : config.getDynomiteStopScript().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("Dynomite server has been stopped");
            instanceState.setStorageProxyAlive(false);
        } else {
            logger.error("Unable to stop Dynomite server. Error code: {}", code);
            logProcessOutput(stopper);
        }
    } catch (Exception e) {
        logger.warn("couldn't shut down Dynomite correctly", e);
    }
}

From source file:com.tw.go.plugin.task.GoPluginImpl.java

private int executeCommand(String workingDirectory, Map<String, String> environmentVariables, String... command)
        throws IOException, InterruptedException {
    ProcessBuilder processBuilder = new ProcessBuilder(command);
    processBuilder.directory(new File(workingDirectory));
    if (environmentVariables != null && !environmentVariables.isEmpty()) {
        processBuilder.environment().putAll(environmentVariables);
    }//from www . j  a v a  2  s. c o m
    Process process = processBuilder.start();

    JobConsoleLogger.getConsoleLogger().readOutputOf(process.getInputStream());
    JobConsoleLogger.getConsoleLogger().readErrorOf(process.getErrorStream());

    return process.waitFor();
}

From source file:org.apache.syncope.installer.utilities.FileSystemUtils.java

public void exec(final String cmd, final String path) {
    try {/*from  ww w.  j av  a  2 s .  c  o  m*/
        handler.logOutput("Executing " + cmd, true);
        final ProcessBuilder builder = new ProcessBuilder(cmd.split(" "));
        if (path != null && !path.isEmpty()) {
            builder.directory(new File(path));
        }
        final Process process = builder.start();
        readResponse(process.getInputStream());
    } catch (final IOException ex) {
        final String errorMessage = "Error executing " + cmd + ": " + ex.getMessage();
        handler.emitError(errorMessage, errorMessage);
        InstallLog.getInstance().error(errorMessage);
    }
}

From source file:com.anrisoftware.globalpom.exec.core.DefaultProcessTask.java

@Override
public ProcessTask call() throws CommandExecException {
    List<String> command = commandLine.getCommand();
    ProcessBuilder builder = new ProcessBuilder(command);
    builder.directory(commandLine.getWorkingDir());
    builder.redirectOutput(Redirect.PIPE);
    builder.redirectError(Redirect.PIPE);
    builder.redirectInput(Redirect.PIPE);
    try {// ww w. ja  v  a2  s  . co m
        startProcess(builder);
    } catch (IOException e) {
        throw log.errorStartCommand(this, e, commandLine);
    } catch (InterruptedException e) {
        throw log.commandInterrupted(this, e, commandLine);
    } catch (ExecutionException e) {
        throw log.errorStartCommand(this, e.getCause(), commandLine);
    }
    return this;
}

From source file:nl.bneijt.javapjson.JavapJsonMojo.java

private String runJavap(File classFile) throws MojoExecutionException {
    File classDirectory = classFile.getParentFile();
    String classFileName = classFile.getName();
    ProcessBuilder psBuilder = new ProcessBuilder("javap", "-l",
            classFileName.substring(0, classFileName.length() - CLASS_EXTENSION.length()));
    psBuilder.directory(classDirectory);
    Process process;//from   w w w  .  ja v a2s .  c  om
    try {
        process = psBuilder.start();
    } catch (IOException e1) {
        throw new MojoExecutionException("Could not start process builder", e1);
    }
    try {
        process.getOutputStream().close();
    } catch (IOException e) {
        throw new MojoExecutionException("Could not close output stream while executing javap");
    }
    try {
        byte[] buffer = new byte[1024];
        IOUtils.read(process.getInputStream(), buffer);
        return new String(buffer);
    } catch (IOException e) {
        throw new MojoExecutionException(
                "Could not read all input from command javap while reading \"" + classFile + "\"");
    }
}

From source file:org.fcrepo.importexport.integration.ExecutableJarIT.java

private Process startJarProcess(final String... args) throws IOException {
    final String[] fullArgs = new String[3 + args.length];
    fullArgs[0] = "java";
    fullArgs[1] = "-jar";
    fullArgs[2] = EXECUTABLE;/*  w  ww. j  a  v  a 2  s .  co  m*/
    System.arraycopy(args, 0, fullArgs, 3, args.length);
    final ProcessBuilder builder = new ProcessBuilder(fullArgs);

    builder.directory(new File(TARGET_DIR));
    final Process process = builder.start();
    logger.debug("Output of jar run: {}", IOUtils.toString(process.getInputStream()));
    return process;
}

From source file:com.smash.revolance.ui.materials.CmdlineHelper.java

public CmdlineHelper exec() throws InterruptedException, IOException {
    ProcessBuilder pb = new ProcessBuilder();

    if (dir != null) {
        pb.directory(dir);
    }/*from  w  ww  .  j  a  v a  2 s .co m*/

    pb.command(cmd);

    pb.redirectError(ProcessBuilder.Redirect.to(err));
    pb.redirectOutput(ProcessBuilder.Redirect.to(out));
    pb.redirectInput(ProcessBuilder.Redirect.from(in));

    System.out.println("Executing cmd: " + cmd[0] + " from dir: " + dir);
    System.out.println("Redirecting out to: " + out.getAbsolutePath());
    System.out.println("Redirecting err to: " + err.getAbsolutePath());

    Process process = pb.start();
    if (sync) {
        process.waitFor();
    }

    this.process = process;

    return this;
}