Example usage for org.apache.commons.exec DefaultExecutor DefaultExecutor

List of usage examples for org.apache.commons.exec DefaultExecutor DefaultExecutor

Introduction

In this page you can find the example usage for org.apache.commons.exec DefaultExecutor DefaultExecutor.

Prototype

public DefaultExecutor() 

Source Link

Document

Default constructor creating a default PumpStreamHandler and sets the working directory of the subprocess to the current working directory.

Usage

From source file:hu.bme.mit.trainbenchmark.sql.process.MySqlProcess.java

public static void runShell(final String shellCommand) throws ExecuteException, IOException {
    final Executor executor = new DefaultExecutor();
    final CommandLine commandLine = new CommandLine("/bin/bash");
    commandLine.addArgument("-c");
    commandLine.addArgument(shellCommand, false);
    executor.execute(commandLine);//w ww . jav a  2  s .  c  o m
}

From source file:net.minecraft.client.MineExec.java

@Override
protected Integer doInBackground() throws IOException, InterruptedException {
    Executor exe = new DefaultExecutor();
    CommandLine mineExec = CommandLine.parse(mineCmd);

    PipedOutputStream stdout = new PipedOutputStream();
    PipedOutputStream stderr = new PipedOutputStream();
    PumpStreamHandler streamHandler = new PumpStreamHandler(stdout, stderr);
    exe.setStreamHandler(streamHandler);
    MinecraftApplet configCrusher = new MinecraftApplet();
    try {// www .ja v a 2 s  .c  om
        File target = new File(VersionInfo.getTechnicFolder());
        BufferedInputStream biserr = new BufferedInputStream(new PipedInputStream(stderr));
        ExecuteResultHandler rh = new DefaultExecuteResultHandler();
        exe.execute(mineExec, rh);
        BufferedReader reader = new BufferedReader(new InputStreamReader(biserr));
        configCrusher.minecraftLoaded();
        String line = reader.readLine();
        logWindow.append(line + "\n");
        int dupLen = 0;
        int dupMax = 25;
        while (line != null) {
            String line2 = reader.readLine();
            if ((line2.contains("Aether") || line2.contains("aether") && watchCfg
                    && new File(target + File.separator + VersionInfo.getModpackName(), "MenuAPI.properties")
                            .exists())) {
                configCrusher.prepareConfigs();
            }
            if ((Boolean) nbDebug[2]) {
                if (!line.equals(line2) || dupLen >= dupMax) {
                    if (dupLen > 0) {
                        logWindow.append(line + "(" + dupLen + ")\n");
                    } else {
                        if (!line.equals(line2)) {
                            logWindow.append(line2 + "\n");
                        }
                    }
                    dupLen = 0;
                } else {
                    dupLen++;
                }
                line = line2;
            } else {
                logWindow.append(line2 + "\n");
                line = line2;
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    if ((Integer) (nbDebug[1]) <= 1) {
        configCrusher.remExtras();
        configCrusher.minecraftClosed(password);
    }
    return 1;
}

From source file:io.rhiot.utils.process.ExecProcessManager.java

@Override
public List<String> executeAndJoinOutput(String... command) {

    CommandLine cmdLine = CommandLine.parse(String.join(" ", command));
    DefaultExecutor executor = new DefaultExecutor();
    executor.setExitValue(0);//ww  w . ja  v a  2  s .c  o m
    ExecResultHandler resultHandler = null;

    if (getTimeout() > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(getTimeout());
        executor.setWatchdog(watchdog);
        resultHandler = new ExecResultHandler(watchdog);
    }
    try {
        CollectingLogOutputStream outAndErr = new CollectingLogOutputStream();
        executor.setStreamHandler(new PumpStreamHandler(outAndErr));
        if (resultHandler != null) {
            executor.execute(cmdLine, resultHandler);
        } else {
            executor.execute(cmdLine);
        }
        resultHandler.waitFor();
        return outAndErr.getLines();

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

From source file:de.torstenwalter.maven.plugins.ExpdpMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    CommandLine commandLine = new CommandLine(expdp);
    addCommonArguments(commandLine);/*  w  w w.j av a 2  s  . c  o  m*/

    getLog().debug("Executing command line: " + obfuscateCredentials(commandLine.toString(), getCredentials()));

    Executor exec = new DefaultExecutor();
    exec.setStreamHandler(new PumpStreamHandler(System.out, System.err));
    try {
        exec.execute(commandLine);
    } catch (ExecuteException e) {
        throw new MojoExecutionException("Command execution failed.", e);
    } catch (IOException e) {
        throw new MojoExecutionException("Command execution failed.", e);
    }
}

From source file:com.dangdang.ddframe.job.plugin.job.type.integrated.ScriptElasticJob.java

@Override
protected void executeJob(final JobExecutionMultipleShardingContext shardingContext) {
    String scriptCommandLine = getJobFacade().getScriptCommandLine();
    Preconditions.checkArgument(!Strings.isNullOrEmpty(scriptCommandLine), "Cannot find script command line.");
    CommandLine cmdLine = CommandLine.parse(scriptCommandLine);
    cmdLine.addArgument(shardingContext.toScriptArguments(), false);
    DefaultExecutor executor = new DefaultExecutor();
    try {/*from  www  .  ja v a 2  s.c o m*/
        executor.execute(cmdLine);
    } catch (final IOException ex) {
        throw new JobException(ex);
    }
}

From source file:com.kotcrab.vis.editor.util.ApplicationUtils.java

public static void startNewInstance() {
    try {// w  w  w.j a va 2 s.  co m
        CommandLine cmdLine = CommandLine.parse(getRestartCommand());
        DefaultExecutor executor = new DefaultExecutor();
        executor.setStreamHandler(new PumpStreamHandler(null, null, null));
        executor.execute(cmdLine, new DefaultExecuteResultHandler());
    } catch (Exception e) {
        Log.exception(e);
    }
}

From source file:de.akquinet.innovation.play.maven.Play2CleanMojo.java

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArgument("clean");
    DefaultExecutor executor = new DefaultExecutor();

    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);/*  w w  w  . j av  a 2  s  .c  om*/
    }

    executor.setWorkingDirectory(project.getBasedir());
    executor.setExitValue(0);
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        throw new MojoExecutionException("Error during cleanup", e);
    }

    // Also delete the dist directory
    File dist = new File(project.getBasedir(), "dist");
    if (dist.exists()) {
        getLog().debug("Deleting " + dist.getAbsolutePath());
        try {
            FileUtils.deleteDirectory(dist);
        } catch (IOException e) {
            throw new MojoExecutionException("Can't delete the dist folder", e);
        }
    } else {
        getLog().debug("'dist' directory not found");
    }

    // Delete the log folder
    File logs = new File(project.getBasedir(), "logs");
    if (logs.exists()) {
        getLog().debug("Deleting " + logs.getAbsolutePath());
        try {
            FileUtils.deleteDirectory(logs);
        } catch (IOException e) {
            throw new MojoExecutionException("Can't delete the logs folder", e);
        }
    } else {
        getLog().debug("'logs' directory not found");
    }

    // Also delete the lib directory if set
    if (cleanLibFolder) {
        File lib = new File(project.getBasedir(), "lib");
        if (lib.exists()) {
            getLog().debug("Deleting " + lib.getAbsolutePath());
            try {
                FileUtils.deleteDirectory(lib);
            } catch (IOException e) {
                throw new MojoExecutionException("Can't delete the " + lib + " folder", e);
            }
        } else {
            getLog().debug("'" + lib + "' directory not found");
        }
    }
}

From source file:de.akquinet.innovation.play.maven.Play2CompilationMojo.java

public void execute() throws MojoExecutionException {

    String line = getPlay2().getAbsolutePath();

    CommandLine cmdLine = CommandLine.parse(line);
    cmdLine.addArgument("compile");
    DefaultExecutor executor = new DefaultExecutor();

    if (timeout > 0) {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
        executor.setWatchdog(watchdog);//from   w ww.  ja  v  a2  s  .  c om
    }

    executor.setExitValue(0);
    executor.setWorkingDirectory(project.getBasedir());
    try {
        executor.execute(cmdLine, getEnvironment());
    } catch (IOException e) {
        throw new MojoExecutionException("Error during compilation", e);
    }
}

From source file:com.creactiviti.piper.plugin.ffmpeg.Mediainfo.java

@Override
public Map<String, Object> handle(Task aTask) throws Exception {
    CommandLine cmd = new CommandLine("mediainfo");
    cmd.addArgument(aTask.getRequiredString("input"));
    log.debug("{}", cmd);
    DefaultExecutor exec = new DefaultExecutor();
    File tempFile = File.createTempFile("log", null);
    try (PrintStream stream = new PrintStream(tempFile);) {
        exec.setStreamHandler(new PumpStreamHandler(stream));
        exec.execute(cmd);/*from   w  ww .j  a v  a  2  s .co  m*/
        return parse(FileUtils.readFileToString(tempFile));
    } catch (ExecuteException e) {
        throw new ExecuteException(e.getMessage(), e.getExitValue(),
                new RuntimeException(FileUtils.readFileToString(tempFile)));
    } finally {
        FileUtils.deleteQuietly(tempFile);
    }
}

From source file:eu.creatingfuture.propeller.blocklyprop.propellent.Propellent.java

public List<String> getPorts() {
    List<String> ports = new ArrayList<>();
    try {/*ww w  . jav  a  2s .  com*/
        CommandLine cmdLine = new CommandLine("propellent/Propellent.exe");
        cmdLine.addArgument("/id");
        cmdLine.addArgument("/gui").addArgument("OFF");
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValues(new int[] { 451, 301 });

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream);
        executor.setStreamHandler(streamHandler);

        try {
            exitValue = executor.execute(cmdLine);
        } catch (ExecuteException ee) {
            //   exitValue = ee.getExitValue();
            logger.log(Level.SEVERE, "Unexpected exit value: {0}", exitValue);
            return ports;
        }

        output = outputStream.toString();

        // 301 = None found
        // 451 = Chip found
        if (exitValue == 301) {
            return ports;
        }

        //            System.out.println("output: " + output);
        Scanner scanner = new Scanner(output);

        Pattern chipFoundPattern = Pattern.compile(".*?(EVT:505).*?");
        Pattern pattern = Pattern.compile(".*?found on (?<comport>[a-zA-Z0-9]*).$");
        while (scanner.hasNextLine()) {
            String portLine = scanner.nextLine();
            if (chipFoundPattern.matcher(portLine).matches()) {
                Matcher portMatch = pattern.matcher(portLine);
                if (portMatch.find()) {
                    String port = portMatch.group("comport");
                    ports.add(port);
                }
            }
        }

        //            System.out.println("output: " + output);
        //            System.out.println("exitValue: " + exitValue);
        return ports;
    } catch (IOException ioe) {
        logger.log(Level.SEVERE, null, ioe);
        return null;
    }
}