Example usage for java.lang ProcessBuilder ProcessBuilder

List of usage examples for java.lang ProcessBuilder ProcessBuilder

Introduction

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

Prototype

ProcessBuilder

Source Link

Usage

From source file:com.att.aro.datacollector.ioscollector.utilities.AppSigningHelper.java

public void executeCmd(String cmd) {
    System.out.println(cmd);//from   www .j a  v a  2s .com
    ProcessBuilder pbldr = new ProcessBuilder();
    if (!Util.isWindowsOS()) {
        pbldr.command(new String[] { "bash", "-c", cmd });
    } else {
        pbldr.command(new String[] { "CMD", "/C", cmd });
    }
    try {
        Process proc = pbldr.start();
        try {
            Thread.sleep(1000 * 2);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        proc.destroy();
    } catch (IOException e) {
        //Do nothing
    }
}

From source file:eu.numberfour.n4js.npmexporter.ui.NpmExportWizard.java

@Override
public boolean performFinish() {

    String destination = exportPage.getDestinationValue();
    List<IProject> toExport = exportPage.getChosenProjects();
    boolean shouldPackAsTarball = exportPage.getShouldPackAsTarball();

    File folder = new File(destination);

    // remap all IProjects
    List<? extends IN4JSProject> toExportIN4JSProjects = mapToIN4JSProjects(toExport);

    if (runTools() && toolRunnerPage.isToolrunRequested()) {
        // bring to front.
        ((WizardDialog) getContainer()).showPage(toolRunnerPage);
    }// w ww.j  a v a 2  s. c o m

    try {

        npmExporter.export(toExportIN4JSProjects, folder);

        if (shouldPackAsTarball) {
            npmExporter.tarAndZip(toExportIN4JSProjects, folder);
        }

        boolean runIt = runTools() && toolRunnerPage.queryRunTool();
        if (runIt) {
            final List<String> toolCommand = toolRunnerPage.getCommand();

            getContainer().run(true, true, new IRunnableWithProgress() {
                @Override
                public void run(IProgressMonitor monitor)
                        throws InvocationTargetException, InterruptedException {
                    try {

                        List<String> cmds = newArrayList();
                        // cmds.add("echo"); // prepend with echo for debug TODO remove
                        // cmds.addAll(toolCommand);
                        cmds.add("bash");
                        cmds.add("-login");
                        cmds.add("-c");
                        // cmds.addAll(toolCommand);
                        cmds.add(Joiner.on(" ").join(toolCommand));

                        System.out.println("Comman will be: " + Joiner.on(" :: ").join(cmds));

                        for (IN4JSProject p : toExportIN4JSProjects) {

                            String info = "Processing " + p.toString() + "\n";
                            System.out.println(info);
                            toolRunnerPage.appendText(info);

                            File dir = npmExporter.exportDestination(p, folder);

                            ProcessBuilder pb = new ProcessBuilder();
                            pb.directory(dir);
                            pb.command(cmds);
                            pb.redirectErrorStream(true);
                            Process proc = pb.start();

                            // handle each of proc's streams in a separate thread
                            ExecutorService handlerThreadPool = Executors.newFixedThreadPool(3);

                            // handlerThreadPool.submit(new Runnable() {
                            // @Override
                            // public void run() {
                            // // we want to write to the stdin of the process
                            // BufferedWriter stdin = new BufferedWriter(
                            // new OutputStreamWriter(proc.getOutputStream()));
                            //
                            // // read from our own stdin so we can write it to proc's stdin
                            // BufferedReader myStdin =
                            // new BufferedReader(new InputStreamReader(System.in));
                            // String line = null;
                            // try {
                            // do {
                            // line = myStdin.readLine();
                            // stdin.write(String.format("%s%n", line));
                            // stdin.flush();
                            // } while(! "exit".equalsIgnoreCase(line));
                            // } catch(IOException e) {
                            // e.printStackTrace();
                            // }
                            // }
                            // });

                            handlerThreadPool.submit(new Runnable() {
                                @Override
                                public void run() {
                                    // we want to read the stdout of the process
                                    BufferedReader stdout = new BufferedReader(
                                            new InputStreamReader(proc.getInputStream()));
                                    String line;
                                    try {
                                        while (null != (line = stdout.readLine())) {
                                            System.err.printf("[stderr] %s%n", line);
                                            toolRunnerPage.appendConsoleOut(line);
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });

                            handlerThreadPool.submit(new Runnable() {
                                @Override
                                public void run() {
                                    // we want to read the stderr of the process
                                    BufferedReader stderr = new BufferedReader(
                                            new InputStreamReader(proc.getErrorStream()));
                                    String line;
                                    try {
                                        while (null != (line = stderr.readLine())) {
                                            System.err.printf("[stderr] %s%n", line);
                                            toolRunnerPage.appendConsoleErr(line);
                                        }
                                    } catch (IOException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });

                            // wait for the process to terminate
                            int exitCode = proc.waitFor();
                            System.out.printf("Process terminated with exit code %d%n", exitCode);
                            handlerThreadPool.shutdown();

                        }

                        // done with all projects.
                        // wait for close.

                        toolRunnerPage.queryCloseDialog();

                    } catch (Exception e) {
                        throw new InvocationTargetException(e);
                    }
                }
            });
        }

    } catch (IOException | ArchiveException | CompressorException e) {

        e.printStackTrace();

        Status s = new Status(ERROR, NpmExporterActivator.PLUGIN_ID, "Error occured during export.", e);
        N4JSActivator.getInstance().getLog().log(s);

        return false;
    } catch (InvocationTargetException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // successfully done, then store relevant history:
    exportPage.finish();
    if (runTools()) {
        toolRunnerPage.finish();
    }

    return true;
}