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:Main.java

public static void exec(File workingDir, String command) {
    try {//from   ww w.  j  a  va 2 s.c  o m
        ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
        processBuilder.directory(workingDir);
        Process process = processBuilder.start();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            System.out.println(line);
        }
        process.waitFor();
        process.destroy();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:design.process.ProcessUtil.java

/***/
public static Process executeProcess(final File workDir, final String... termArray)
        throws IOException, InterruptedException {
    final List<String> termList = Arrays.asList(termArray);
    final ProcessBuilder builder = new ProcessBuilder(termList);
    final Process process = builder.directory(workDir).start();
    process.waitFor();/*  w w  w .  j  ava 2  s .c o m*/
    return process;
}

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);
    builder.redirectErrorStream(false);/* w w w  .j a  va 2 s .co m*/
    builder.command(command);
    return builder.start();
}

From source file:myproject.Model.Common.ProcessExecutor.java

private static String properExecute(File file, String... commands) throws IOException, InterruptedException {
    ProcessBuilder builder = new ProcessBuilder(commands);
    if (file != null) {
        builder.directory(file);
    }//from  w  ww . ja va 2 s.c  o  m
    Process process = builder.start();
    String input = IOUtils.toString(process.getInputStream(), "utf-8");
    String error = IOUtils.toString(process.getErrorStream(), "utf-8");
    //String command = Arrays.toString(builder.command().toArray(new String[] {}));
    process.waitFor();
    process.getInputStream().close();
    if (!error.isEmpty()) {
        Log.errorLog(error, ProcessExecutor.class);
    }
    String result = input;
    if (input.isEmpty()) {
        result = error;
    }
    return result;
}

From source file:ml.shifu.shifu.executor.ProcessManager.java

public static int runShellProcess(String currentDir, String[] args) throws IOException {
    ProcessBuilder processBuilder = new ProcessBuilder(args);
    processBuilder.directory(new File(currentDir));
    processBuilder.redirectErrorStream(true);
    Process process = processBuilder.start();

    LogThread logThread = new LogThread(process, process.getInputStream(), currentDir);
    logThread.start();//from  w  w  w.  j a va2 s .co m

    try {
        process.waitFor();
    } catch (InterruptedException e) {
        process.destroy();
    } finally {
        logThread.setToQuit(true);
    }

    LOG.info("Under {} directory, finish run `{}`", currentDir, args);
    return process.exitValue();
}

From source file:gool.executor.Command.java

/**
 * Executes a command in the specified working directory.
 * /*from   w  w  w .j a  v  a  2s  . c o  m*/
 * @param workingDir
 *            the working directory.
 * @param params
 *            the command to execute and its parameters.
 * @return the console output.
 */
public static String exec(File workingDir, List<String> params, Map<String, String> env) {
    try {
        StringBuffer buffer = new StringBuffer();

        ProcessBuilder pb = new ProcessBuilder(params);
        pb.directory(workingDir);

        for (Entry<String, String> e : env.entrySet()) {
            pb.environment().put(e.getKey(), e.getValue());
        }
        Process p = pb.redirectErrorStream(true).start();

        p.getOutputStream().close();
        BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line;
        while ((line = in.readLine()) != null) {
            buffer.append(line).append("\n");
        }

        int retval = p.waitFor();

        if (retval != 0) {
            throw new CommandException(
                    "The command execution returned " + retval + " as return value... !\n" + buffer);
        }

        return buffer.toString();
    } catch (IOException e) {
        throw new CommandException(e);
    } catch (InterruptedException e) {
        throw new CommandException("It seems the process was killed", e);
    }
}

From source file:Main.java

public static String runCommand(String[] command, String workdirectory) {
    String result = "";
    Log.d("AppUtil.class", "#" + command);
    try {/*  w ww.  ja  v a 2 s .  co  m*/
        ProcessBuilder builder = new ProcessBuilder(command);
        // set working directory
        if (workdirectory != null) {
            builder.directory(new File(workdirectory));
        }
        builder.redirectErrorStream(true);
        Process process = builder.start();
        InputStream in = process.getInputStream();
        byte[] buffer = new byte[1024];
        while (in.read(buffer) != -1) {
            String str = new String(buffer);
            result = result + str;
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.googlecode.promnetpp.research.main.CompareOutputMain.java

private static void doSeedRun(int seed) throws IOException, InterruptedException {
    System.out.println("Running with seed " + seed);
    String pattern = "int random = <INSERT_SEED_HERE>;";
    int start = sourceCode.indexOf(pattern);
    int end = start + pattern.length();
    String line = sourceCode.substring(start, end);
    line = line.replace("<INSERT_SEED_HERE>", Integer.toString(seed));
    String sourceCodeWithSeed = sourceCode.replace(pattern, line);
    File tempFile = new File("temp.pml");
    FileUtils.writeStringToFile(tempFile, sourceCodeWithSeed);
    //Create a "project" folder
    String fileNameWithoutExtension = fileName.split("[.]")[0];
    File folder = new File("test1-" + fileNameWithoutExtension);
    if (folder.exists()) {
        FileUtils.deleteDirectory(folder);
    }/*from  w  ww  .  jav a2s. co  m*/
    folder.mkdir();
    //Copy temp.pml to our new folder
    FileUtils.copyFileToDirectory(tempFile, folder);
    //Simulate the model using Spin
    List<String> spinCommand = new ArrayList<String>();
    spinCommand.add(GeneralData.spinHome + "/spin");
    spinCommand.add("-u1000000");
    spinCommand.add("temp.pml");
    ProcessBuilder processBuilder = new ProcessBuilder(spinCommand);
    processBuilder.directory(folder);
    processBuilder.redirectOutput(new File(folder, "spin-" + seed + ".txt"));
    Process process = processBuilder.start();
    process.waitFor();
    //Translate via PROMNeT++
    List<String> PROMNeTppCommand = new ArrayList<String>();
    PROMNeTppCommand.add("java");
    PROMNeTppCommand.add("-enableassertions");
    PROMNeTppCommand.add("-jar");
    PROMNeTppCommand.add("\"" + GeneralData.getJARFilePath() + "\"");
    PROMNeTppCommand.add("temp.pml");
    processBuilder = new ProcessBuilder(PROMNeTppCommand);
    processBuilder.directory(folder);
    processBuilder.environment().put("PROMNETPP_HOME", GeneralData.PROMNeTppHome);
    process = processBuilder.start();
    process.waitFor();
    //Run opp_makemake
    FileUtils.copyFileToDirectory(new File("opp_makemake.bat"), folder);
    List<String> makemakeCommand = new ArrayList<String>();
    if (Utilities.operatingSystemType.equals("windows")) {
        makemakeCommand.add("cmd");
        makemakeCommand.add("/c");
        makemakeCommand.add("opp_makemake.bat");
    } else {
        throw new RuntimeException("Support for Linux/OS X not implemented" + " here yet.");
    }
    processBuilder = new ProcessBuilder(makemakeCommand);
    processBuilder.directory(folder);
    process = processBuilder.start();
    process.waitFor();
    //Run make
    FileUtils.copyFileToDirectory(new File("opp_make.bat"), folder);
    List<String> makeCommand = new ArrayList<String>();
    if (Utilities.operatingSystemType.equals("windows")) {
        makeCommand.add("cmd");
        makeCommand.add("/c");
        makeCommand.add("opp_make.bat");
    } else {
        throw new RuntimeException("Support for Linux/OS X not implemented" + " here yet.");
    }
    processBuilder = new ProcessBuilder(makeCommand);
    processBuilder.directory(folder);
    process = processBuilder.start();
    process.waitFor();
    System.out.println(Utilities.getStreamAsString(process.getInputStream()));
    System.exit(1);
}

From source file:Main.java

public static String runCommand(String[] command, String workdirectory) {
    String result = "";
    //AbLogUtil.d(AbAppUtil.class, "#"+command);
    try {/*w  w w . j  av a 2  s .  c o m*/
        ProcessBuilder builder = new ProcessBuilder(command);
        // set working directory
        if (workdirectory != null) {
            builder.directory(new File(workdirectory));
        }
        builder.redirectErrorStream(true);
        Process process = builder.start();
        InputStream in = process.getInputStream();
        byte[] buffer = new byte[1024];
        while (in.read(buffer) != -1) {
            String str = new String(buffer);
            result = result + str;
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:br.on.daed.services.pdf.DadosMagneticos.java

public static byte[] gerarPDF(String ano, String tipo)
        throws IOException, InterruptedException, UnsupportedOperationException {

    File tmpFolder;//from w  w  w.  j  a  v  a 2 s  . co m
    String folderName;

    Double anoDouble = Double.parseDouble(ano);
    List<String> dados = Arrays.asList(TIPO_DADOS_MAGNETICOS);

    byte[] ret = null;

    if (anoDouble >= ANO_MAGNETICO[0] && anoDouble < ANO_MAGNETICO[1] && dados.contains(tipo)) {

        do {

            folderName = "/tmp/dislin" + Double.toString(System.currentTimeMillis() * Math.random());
            tmpFolder = new File(folderName);

        } while (tmpFolder.exists());

        tmpFolder.mkdir();

        ProcessBuilder processBuilder = new ProcessBuilder("/opt/declinacao-magnetica/./gerar", ano, tipo);

        processBuilder.directory(tmpFolder);

        processBuilder.environment().put("LD_LIBRARY_PATH", "/usr/local/dislin");

        Process proc = processBuilder.start();

        proc.waitFor();

        ProcessHelper.outputProcess(proc);

        File arquivoServido = new File(folderName + "/dislin.pdf");

        FileInputStream fis = new FileInputStream(arquivoServido);

        ret = IOUtils.toByteArray(fis);

        processBuilder = new ProcessBuilder("rm", "-r", folderName);

        tmpFolder = new File("/");

        processBuilder.directory(tmpFolder);

        Process delete = processBuilder.start();

        delete.waitFor();

    } else {
        throw new UnsupportedOperationException("Entrada invlida");
    }
    return ret;
}