List of usage examples for org.apache.commons.exec CommandLine addArgument
public CommandLine addArgument(final String argument, final boolean handleQuoting)
From source file:hu.bme.mit.trainbenchmark.sql.process.MySqlProcess.java
public static void runScript(final String scriptFile) throws ExecuteException, IOException { final Executor executor = new DefaultExecutor(); final CommandLine commandLine = new CommandLine("/bin/bash"); commandLine.addArgument(scriptFile, false); executor.execute(commandLine);/*from ww w .j a va2 s .c om*/ }
From source file:com.stratio.ingestion.utils.IngestionUtils.java
public static DefaultExecuteResultHandler executeBash(String command) throws IOException { CommandLine cmdLine = CommandLine.parse("bash"); cmdLine.addArgument("-c", false); cmdLine.addArgument(command, false); DefaultExecutor executor = new DefaultExecutor(); ByteArrayOutputStream outputStreamAgentStart = new ByteArrayOutputStream(); executor.setStreamHandler(new PumpStreamHandler(outputStreamAgentStart)); DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler(); executor.execute(cmdLine, handler);//from w w w. j a v a 2s . c om return handler; }
From source file:com.jaeksoft.searchlib.util.ExecuteUtils.java
final public static int command(File workingDirectory, String cmd, String classpath, boolean setJavaTempDir, OutputStream outputStream, OutputStream errorStream, Long timeOut, String... arguments) throws ExecuteException, IOException { Map<String, String> envMap = null; if (classpath != null) { envMap = new HashMap<String, String>(); envMap.put("CLASSPATH", classpath); }//from www . j a v a 2 s. com CommandLine commandLine = new CommandLine(cmd); if (setJavaTempDir) if (!StringUtils.isEmpty(SystemUtils.JAVA_IO_TMPDIR)) commandLine.addArgument(StringUtils.fastConcat("-Djava.io.tmpdir=", SystemUtils.JAVA_IO_TMPDIR), false); if (arguments != null) for (String argument : arguments) commandLine.addArgument(argument); DefaultExecutor executor = new DefaultExecutor(); if (workingDirectory != null) executor.setWorkingDirectory(workingDirectory); if (outputStream != null) { PumpStreamHandler pumpStreamHandler = new PumpStreamHandler(outputStream, errorStream); executor.setStreamHandler(pumpStreamHandler); } if (timeOut != null) { ExecuteWatchdog watchdog = new ExecuteWatchdog(timeOut); executor.setWatchdog(watchdog); } return envMap != null ? executor.execute(commandLine, envMap) : executor.execute(commandLine); }
From source file:io.manasobi.utils.CmdUtils.java
/** * ? ?? ? ? ?/* w w w .j a v a 2 s . c o m*/ * * @param line * @param arguments ? * @return int ? */ public static int execute(String line, String[] arguments) { CommandLine commandLine = CommandLine.parse(line); for (String argument : arguments) { commandLine.addArgument(argument, false); } return execute(commandLine, null); }
From source file:io.selendroid.standalone.android.impl.DefaultAndroidEmulator.java
public static List<AndroidEmulator> listAvailableAvds() throws AndroidDeviceException { List<AndroidEmulator> avds = Lists.newArrayList(); CommandLine cmd = new CommandLine(AndroidSdk.android()); cmd.addArgument("list", false); cmd.addArgument("avds", false); String output = null;/*from w w w .j av a 2 s. co m*/ try { output = ShellCommand.exec(cmd, 20000); } catch (ShellCommandException e) { throw new AndroidDeviceException(e); } Map<String, Integer> startedDevices = mapDeviceNamesToSerial(); String[] avdsOutput = StringUtils.splitByWholeSeparator(output, "---------"); if (avdsOutput != null && avdsOutput.length > 0) { for (String element : avdsOutput) { if (!element.contains("Name:")) { continue; } DefaultAndroidEmulator emulator = new DefaultAndroidEmulator(element); if (startedDevices.containsKey(emulator.getAvdName())) { emulator.setSerial(startedDevices.get(emulator.getAvdName())); } avds.add(emulator); } } return avds; }
From source file:io.selendroid.android.impl.DefaultAndroidEmulator.java
public static List<AndroidEmulator> listAvailableAvds() throws AndroidDeviceException { List<AndroidEmulator> avds = Lists.newArrayList(); CommandLine cmd = new CommandLine(AndroidSdk.android()); cmd.addArgument("list", false); cmd.addArgument("avds", false); String output = null;//from w w w . j a v a 2s . c om try { output = ShellCommand.exec(cmd, 20000); } catch (ShellCommandException e) { throw new AndroidDeviceException(e); } Map<String, Integer> startedDevices = mapDeviceNamesToSerial(); String[] avdsOutput = StringUtils.splitByWholeSeparator(output, "---------"); if (avdsOutput != null && avdsOutput.length > 0) { for (int i = 0; i < avdsOutput.length; i++) { if (avdsOutput[i].contains("Name:") == false) { continue; } String element = avdsOutput[i]; String avdName = extractValue("Name: (.*?)$", element); String abi = extractValue("ABI: (.*?)$", element); String screenSize = extractValue("Skin: (.*?)$", element); String target = extractValue("\\(API level (.*?)\\)", element); File avdFilePath = new File(extractValue("Path: (.*?)$", element)); DefaultAndroidEmulator emulator = new DefaultAndroidEmulator(avdName, abi, screenSize, target, avdFilePath); if (startedDevices.containsKey(avdName)) { emulator.setSerial(startedDevices.get(avdName)); } avds.add(emulator); } } return avds; }
From source file:com.github.genium_framework.appium.support.command.CommandManager.java
/** * Constructs a CommandLine object depending on the current running * operating system using the number of arguments passed to it. * * @param command The native OS command to run or an absolute path to an * executable to run.//from www . j av a 2s .c om * @param parameters The command parameters. * @param arguments String arguments of the command to formulate from. * @return CommandLine object that represents the command you want to * execute. */ public static CommandLine createCommandLine(String command, String[] parameters, String... arguments) { CommandLine commanddLine = null; // add the command to be executed if (OS.isFamilyWindows()) { commanddLine = new CommandLine("\"" + command + "\""); } else if (OS.isFamilyMac() || OS.isFamilyUnix()) { commanddLine = new CommandLine(command.contains(" ") ? "'" + command + "'" : command); } else { throw new UnsupportedOperationException("Unsupported operating system."); } // add the command parameters if (OS.isFamilyWindows()) { for (String parameter : parameters) { commanddLine.addArgument("\"" + parameter + "\"", false); } } else if (OS.isFamilyMac() || OS.isFamilyUnix()) { for (String parameter : parameters) { commanddLine.addArgument(parameter.contains(" ") ? "'" + parameter + "'" : parameter, false); } } // add the command arguments for (String argument : arguments) { // you have to pass the false value and disable handling quoting // otherwise the OS won't be able to run the shell file on MAc OS commanddLine.addArgument(argument, false); } return commanddLine; }
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 {/* w ww .ja va 2s . c om*/ executor.execute(cmdLine); } catch (final IOException ex) { throw new JobException(ex); } }
From source file:com.dangdang.ddframe.job.executor.type.ScriptJobExecutor.java
private void executeScript(final ShardingContext shardingContext, final String scriptCommandLine) { CommandLine commandLine = CommandLine.parse(scriptCommandLine); commandLine.addArgument(GsonFactory.getGson().toJson(shardingContext), false); try {/*from w w w . j av a2s.co m*/ new DefaultExecutor().execute(commandLine); } catch (final IOException ex) { throw new JobConfigurationException("Execute script failure.", ex); } }
From source file:com.dangdang.ddframe.job.api.type.script.executor.ScriptJobExecutor.java
@Override protected void process(final ShardingContext shardingContext) { String scriptCommandLine = ((ScriptJobConfiguration) getJobRootConfig().getTypeConfig()) .getScriptCommandLine();/*from w w w. ja v a2 s. co m*/ if (Strings.isNullOrEmpty(scriptCommandLine)) { getJobExceptionHandler().handleException(getJobName(), new JobConfigurationException( "Cannot find script command line for job '{}', job is not executed.", shardingContext.getJobName())); return; } CommandLine commandLine = CommandLine.parse(scriptCommandLine); commandLine.addArgument(GsonFactory.getGson().toJson(shardingContext), false); try { executor.execute(commandLine); } catch (final IOException ex) { getJobExceptionHandler().handleException(getJobName(), ex); } }