Executor exec = new DefaultExecutor(); CommandLine cl = new CommandLine("sed"); cl.addArgument("s/hello/goodbye/"); String text = "hello"; ByteArrayInputStream input = new ByteArrayInputStream(text.getBytes("ISO-8859-1")); ByteArrayOutputStream output = new ByteArrayOutputStream(); exec.setStreamHandler(new PumpStreamHandler(output, null, input)); exec.execute(cl); System.out.println("result: " + output.toString("ISO-8859-1"));
Executor executor = new Executors.newFixedThreadPool(NUMBER_OF_THREADS);
Executor exec = new DefaultExecutor(); CommandLine cl = CommandLine.parse(command); Map<String, String> env = EnvironmentUtils.getProcEnvironment(); env.put("XYZ", "XYZ"); //own vars exec.execute(cl, env);
private static final BlockingQueue<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(10); /** * An {@link Executor} that can be used to execute tasks in parallel. */ public static final Executor THREAD_POOL_EXECUTOR = new ThreadPoolExecutor(CORE_POOL_SIZE, MAXIMUM_POOL_SIZE, KEEP_ALIVE, TimeUnit.SECONDS, sPoolWorkQueue, sThreadFactory); /** * An {@link Executor} that executes tasks one at a time in serial * order. This serialization is global to a particular process. */ public static final Executor SERIAL_EXECUTOR = new SerialExecutor();
private static final Executor executor = new MyThreadPoolExecutor(10, 10, 50000L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(100));
Executor ex = new DefaultExecutor(); // Create an output stream and set it as the process' input OutputStream out = new ByteArrayOutputStream(); ex.getStreamHandler().setProcessInputStream(out); ... try { ex.execute(cl); out.write("\n".getBytes()); // TODO use appropriate charset explicitly ...
Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(new File("C:\\My\\Dir\\")); CommandLine cl = new CommandLine("mybatch.bat"); int exitvalue = exec.execute(cl);
final ArrayBloclingQueue<PlayerPayoffs> queue=new ArrayBloclingQueue<PlayerPayoffs>(); Executor exec=new Executors.newFixedThreadPool(N); // number of threads depends on hardware for (int i = 0; i < GameConstants.MAX_GAMES; i++) { exec.execute(new Runnable(){ EraserSimulator eraser = new EraserSimulator(GameConstants.MAX_TARGETS, GameConstants.MAX_RESOURCES, GameConstants.NUM_ATTACKER_TYPES, seed.nextInt()); Map<Set<SingleObjectiveTarget>, Double> gameStrategy = eraser.run(); assert (gameStrategy != null); TestingGameSimulator testingGame = new TestingGameSimulator(GameConstants.MAX_TARGETS, gameStrategy, GameConstants.NUM_GAMES_TO_STORE_FOR_HISTORY, GameConstants.NUM_TESTING_GAMES_TO_PLAY); PlayerPayoffs payoffs = testingGame.run(eraser.getEraserInstance()); queue.put(payoffs); }); }
... import org.apache.commons.exec.CommandLine; import org.apache.commons.exec.DefaultExecutor; import org.apache.commons.exec.Executor; import org.apache.commons.io.FileUtils; ... CommandLine cmd = new CommandLine("path/to/shellscript.sh"); cmd.addArgument("foo"); cmd.addArgument("bar"); Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(FileUtils.getUserDirectory()); exec.execute(cmd); ...
Executor exec = new DefaultExecutor(); exec.setWorkingDirectory(file); CommandLine cl = new CommandLine("./runCommand.sh"); int exitvalue = exec.execute(cl); if (exitvalue == 0) System.out.println("./runCommand.sh succeeded....");
private Result mResult; private OnResultListener mListener; private void setResult(Result result, OnResultListener listener){ this.mResult = result; mListener = listener; } private Result getResult(){ new Executor(new OnResultListener() { @Override public void onResult(Result result) { if (mListener != null) { mListener.onResult(result); } setResult(result); } }).execute(); return mResult; }
CommandLine cmdLine = new CommandLine(yourExecutable); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); Executor executor = new DefaultExecutor(); executor.setStreamHandler(new YourPumpStreamHandler()); executor.execute(cmdLine, resultHandler); // until waitFor returns, the process is running and events can be generated int exitValue = resultHandler.waitFor();
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); Executor executor = new DefaultExecutor(); executor.execute(cmdLine, resultHandler); int exitValue = resultHandler.waitFor(); return exitValue;
CommandLine cmdLine = CommandLine.parse("C:\\Program Files\\MySQL\\bin\\mysqldump"); cmdLine.addArgument("--user="); // add further args here Executor executor = new DefaultExecutor(); OutputStream stdOut = new ByteArrayOutputStream(); PumpStreamHandler psh = new PumpStreamHandler(stdOut); executor.setStreamHandler(psh); try { int exitCode = executor.execute(cmdLine); String output = stdOut.toString(); BufferedReader lineReader = new BufferedReader(new StringReader(output)); String line; while ((line = lineReader.readLine()) != null) { log.debug("Reading line #{} - '{}'", lineCount, line); } } catch (IOException e) { // handle the exception }
Executor ex = new DefaultExecutor(); ex.setStreamHandler(new PumpStreamHandler(new LogOutputStream() { @Override protected void processLine(String line, int level) { // Call PircBot's sendMessage() passing line } })); int exitValue = ex.execute("command to execute locally");
@Test public void testDoAsync() { Executor executor = new SynchronousExecutor(); Foo objectToTest = new Foo(executor); Callback callback = mock(Callback.class); objectToTest.doAsync(callback); // Verify that Callback#onComplete was called using Mockito. verify(callback).onComplete(any(Data.class)); // Assert that we got back the data that we expected. assertEquals(expectedData, callback.getData()); }
ByteArrayOutputStream os = new ByteArrayOutputStream(); PumpStreamHandler ps = new PumpStreamHandler(os); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); String str = avconv_command; CommandLine commandLine = CommandLine.parse(str); ExecuteWatchdog watchdog = new ExecuteWatchdog(1000000); Executor executor = new DefaultExecutor(); executor.setWatchdog(watchdog); executor.setStreamHandler(ps); executor.execute(commandLine, resultHandler); Reader reader = new InputStreamReader(new ByteArrayInputStream(os.toByteArray())); BufferedReader r = new BufferedReader(reader); String tmp = null; while ((tmp = r.readLine()) != null) { //Do something with tmp line }
String line = "notepad.exe"; CommandLine cmdLine = CommandLine.parse(line); ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout); Executor exec = new DefaultExecutor(); DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler(); exec.execute(cmdLine, handler); // wait for script to run Thread.sleep(2000); // terminate it manually if needed if (someCondition) { watchdog.destroyProcess(); } // wait until the result of the process execution is propagated handler.waitFor(WAITFOR_TIMEOUT); System.out.println("Process exitValue: " + handler.getExitValue());
CommandLine cmdLine = new CommandLine("AcroRd32.exe"); cmdLine.addArgument("/p"); cmdLine.addArgument("/h"); cmdLine.addArgument("${file}"); HashMap map = new HashMap(); map.put("file", new File("invoice.pdf")); commandLine.setSubstitutionMap(map); DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); ExecuteWatchdog watchdog = new ExecuteWatchdog(60*1000); Executor executor = new DefaultExecutor(); executor.setExitValue(1); executor.setWatchdog(watchdog); executor.execute(cmdLine, resultHandler); // some time later the result handler callback was invoked so we // can safely request the exit value int exitValue = resultHandler.waitFor()
String cmdline = "mysql"; DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); CommandLine commandLine = CommandLine.parse(cmdline); commandLine.addArgument("--user=" + dbuser); commandLine.addArgument("--password=" + dbpass); commandLine.addArgument("--force"); commandLine.addArgument("-v"); commandLine.addArgument("-e"); commandLine.addArgument("source"); commandLine.addArgument(backUpFile); commandLine.addArgument(database); Executor executor = new DefaultExecutor(); executor.execute(commandLine, resultHandler); resultHandler.waitFor();