List of usage examples for java.lang ProcessBuilder start
public Process start() throws IOException
From source file:Main.java
/** * Method to find the number of files from a particular folder on the device. *///from w w w. j a v a 2 s . co m public static int findNumberOfFiles(String dFolderPath) throws InterruptedException { int numOfFiles = 0; Thread.sleep(500); try { ProcessBuilder process = new ProcessBuilder("adb", "shell", "ls", dFolderPath, "|", "wc", "-l"); Process p = process.start(); //Thread.sleep(5000); BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; p.waitFor(); while ((line = br.readLine()) != null) { if (!line.equals("")) numOfFiles = Integer.parseInt(line); } } catch (Exception e) { System.out.println(e); } return numOfFiles; }
From source file:cu.uci.uengine.utils.FileUtils.java
public static boolean dos2unixFileFixer(String filePath) throws IOException, InterruptedException { // bash es muy sensible a los cambios de linea \r\n de Windows. Para // prevenir que esto cause Runtime Errors, es necesario convertirlos a // un sistema comprensible por ubuntu: \n normal en linux. El comando // dos2unix hace esto. // se lo dejamos a todos los codigos para evitar que algun otro lenguaje // tambien padezca de esto Properties langProps = new Properties(); langProps.load(ClassLoader.getSystemResourceAsStream("uengine.properties")); String dos2unixPath = langProps.getProperty("dos2unix.path"); ProcessBuilder pb = new ProcessBuilder(dos2unixPath, filePath); Process process = pb.start(); process.waitFor();/*from w w w . j a v a 2 s. c o m*/ return process.exitValue() == 0; }
From source file:Main.java
public static String getLocalMacAddress(Context mc) { String defmac = "00:00:00:00:00:00"; InputStream input = null;/*from w ww. j a va2 s. com*/ String wifimac = getWifiMacAddress(mc); if (null != wifimac) { if (!wifimac.equals(defmac)) return wifimac; } try { ProcessBuilder builder = new ProcessBuilder("busybox", "ifconfig"); Process process = builder.start(); input = process.getInputStream(); byte[] b = new byte[1024]; StringBuffer buffer = new StringBuffer(); while (input.read(b) > 0) { buffer.append(new String(b)); } String value = buffer.substring(0); String systemFlag = "HWaddr "; int index = value.indexOf(systemFlag); // List <String> address = new ArrayList <String> (); if (0 < index) { value = buffer.substring(index + systemFlag.length()); // address.add(value.substring(0,18)); defmac = value.substring(0, 17); } } catch (Exception e) { e.printStackTrace(); } return defmac; }
From source file:com.twitter.hraven.hadoopJobMonitor.notification.Mail.java
public static void send(String subject, String msg, String to) { List<String> commands = new LinkedList<String>(); commands.add("/bin/bash"); commands.add("-c"); commands.add("echo \"" + msg + "\" | mail -s \"" + subject + "\" " + to + "," + CC); //This option is not supported by all mail clients: + " -c " + CC); //This option is not supported by all mail clients: + " -- -f " + ADMIN); ProcessBuilder pb = new ProcessBuilder(commands); Process p;//w w w. jav a 2 s .c o m int exitValue = -1; try { p = pb.start(); exitValue = p.waitFor(); LOG.info("Send email to " + to + " exitValue is " + exitValue); } catch (IOException e) { LOG.error("Error in executing mail command", e); } catch (InterruptedException e) { LOG.error("Error in executing mail command", e); } finally { if (exitValue != 0) { LOG.fatal(commands); // ExitUtil.terminate(1, "Could not send mail: " + "exitValue was " // + exitValue); } } }
From source file:Main.java
public static void exec(File workingDir, String command) { try {/*from w w w . j a va2 s . com*/ 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:Main.java
public static List<String> listAvailableIosPlatformVersions() { List<String> results = new ArrayList<String>(); try {//w w w. ja v a 2s .c o m ProcessBuilder pb = new ProcessBuilder("xcodebuild", "-showsdks"); pb.redirectErrorStream(true); Process p = pb.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; Pattern pattern = Pattern.compile("-sdk iphoneos(([0-9]+.?)+)"); while ((line = reader.readLine()) != null) { Matcher m = pattern.matcher(line); while (m.find()) { results.add(m.group(1)); } } } catch (Throwable t) { } return results; }
From source file:Main.java
public static Process runCommand(String... arguments) { List<String> commandLine = Lists.asList(getAdbPath(getSdkPath()), arguments); ProcessBuilder processBuilder = new ProcessBuilder(commandLine); try {//w ww. j a v a2s .com return processBuilder.start(); } catch (IOException e) { throw new RuntimeException("An IOException occurred when starting ADB.", e); } }
From source file:de.micromata.mgc.application.webserver.config.KeyTool.java
public static void generateKey(ValContext ctx, File keyFile, String storePass, String keyAlias) { String[] args = { "keytool", "-genkey", "-alias", keyAlias, "-keyalg", "RSA", "-keystore", keyFile.getAbsolutePath(), "-keysize", "2048", "-keypass", storePass, "-storepass", storePass, "-dname", "cn=Launcher, ou=MGC, o=Microamta, c=DE" }; StringBuilder oksb = new StringBuilder(); oksb.append("Execute: " + StringUtils.join(args, " ")); try {/*from w ww . j ava 2 s. c om*/ ProcessBuilder pb = new ProcessBuilder(args); pb.redirectErrorStream(true); Process process = pb.start(); InputStream is = process.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { oksb.append(line); } boolean success = process.waitFor(5, TimeUnit.SECONDS); if (success == false) { ctx.directError(null, "Fail to wait for keytool"); } else { int exitValue = process.exitValue(); if (exitValue == 0) { oksb.append("\nSuccess"); ctx.directInfo(null, oksb.toString()); } else { ctx.directError(null, oksb.toString()); ctx.directError(null, "Failure executing keytool. ReturnCode: " + exitValue); } } } catch (Exception ex) { ctx.directError(null, "Failure executing keytool: " + ex.getMessage(), ex); } }
From source file:com.acmutv.ontoqa.tool.runtime.RuntimeManager.java
/** * Executes the given command and arguments. * @param command The command to execute. Arguments must be given as a separated strings. * E.g.: BashExecutor.runCommand("ls", "-la") or BashExecutor.runCommand("ls", "-l", "-a") * @return The command output as a string. * @throws IOException when error in process generation or output. */// w w w . j a va 2 s . com public static String runCommand(String... command) throws IOException { LOGGER.trace("command={}", Arrays.asList(command)); String out; ProcessBuilder pb = new ProcessBuilder(command); Process p = pb.start(); try (InputStream in = p.getInputStream()) { out = IOUtils.toString(in, Charset.defaultCharset()); } return out.trim(); }
From source file:Main.java
public static boolean isAndroidEmulatorRunning(File androidSdkHome) throws IOException { if (androidSdkHome == null) { return false; }//w w w .ja va 2s . co m File adb = new File(androidSdkHome, "platform-tools" + File.separator + "adb"); if (!adb.exists()) { return false; } boolean isEmulatorRunning = false; ProcessBuilder pb = new ProcessBuilder(adb.getAbsolutePath(), "devices"); pb.redirectErrorStream(true); Process p = pb.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { line = line.trim(); if (line.startsWith("List of devices")) { continue; } else if (line.startsWith("emulator-")) { String[] tokens = line.split("\\s"); String name = tokens[0]; String status = tokens[1]; int port = Integer.parseInt(name.substring(name.indexOf("-") + 1)); if (status.equals("device") && port == 5560) { isEmulatorRunning = true; } } } return isEmulatorRunning; }