List of usage examples for java.lang Process getInputStream
public abstract InputStream getInputStream();
From source file:com.junoyoon.BullsUtil.java
/** * Run command and/* w w w . j a va2 s. co m*/ * * @param cmd * @return * @throws IOException */ public static String getCmdOutput(List<String> cmd) throws IOException { ProcessBuilder builder = new ProcessBuilder(); builder.command().addAll(cmd); builder.redirectErrorStream(true); StringBuilder result = new StringBuilder(1024); Process proc = builder.start(); boolean firstLine = true; for (Object eachLineObject : IOUtils.readLines(proc.getInputStream())) { String eachLine = (String) eachLineObject; if (firstLine) { eachLine = eachLine.replace("charset=us-ascii", "charset=" + Constant.DEFAULT_ENCODING); firstLine = false; } result.append(eachLine).append("\n"); } return result.toString(); }
From source file:Main.java
public static String execRootCmd(String cmd) { String result = "result : "; try {//from ww w . jav a2 s. co m Process p = Runtime.getRuntime().exec("su "); OutputStream outStream = p.getOutputStream(); DataOutputStream dOutStream = new DataOutputStream(outStream); InputStream inStream = p.getInputStream(); DataInputStream dInStream = new DataInputStream(inStream); String str1 = String.valueOf(cmd); String str2 = str1 + "\n"; dOutStream.writeBytes(str2); dOutStream.flush(); String str3 = null; String line = ""; while ((line = dInStream.readLine()) != null) { Log.d("result", str3); str3 += line; } dOutStream.writeBytes("exit\n"); dOutStream.flush(); p.waitFor(); return result; } catch (Exception e) { e.printStackTrace(); return result; } }
From source file:Main.java
public static String getLocalDns(String dns) { Process process = null; String str = ""; BufferedReader reader = null; try {//from w w w. ja v a 2 s. c o m process = Runtime.getRuntime().exec("getprop net." + dns); reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { str += line; } reader.close(); process.waitFor(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } process.destroy(); } catch (Exception e) { } } return str.trim(); }
From source file:com.pentaho.ctools.utils.BAServerService.java
/** * This method shall execute a windows command to start/stop/query * windows services//from w w w . j av a2 s . c o m * * @param command * @return Output of the command execution. */ private static String ExecuteCommand(String[] command) { String output = ""; try { Process process = new ProcessBuilder(command).start(); try (InputStream inputStream = process.getInputStream(); InputStreamReader inputStreamReader = new InputStreamReader(inputStream); BufferedReader bufferedReader = new BufferedReader(inputStreamReader);) { String line; while ((line = bufferedReader.readLine()) != null) { LOG.debug("OUTPUT:: " + line); output += line; } } catch (Exception ex) {//InputStream LOG.debug("InputStream Exception : " + ex); } } catch (Exception ex) { //Process LOG.debug("Process Exception : " + ex); } return output; }
From source file:gool.executor.Command.java
/** * Executes a command in the specified working directory. * /*w w w.j a va2 s. co m*/ * @param workingDir * the working directory. * @param params * the command to execute and its parameters. * @return the console output. */ public static String exec(File workingDir, List<String> params, Map<String, String> env) { try { StringBuffer buffer = new StringBuffer(); ProcessBuilder pb = new ProcessBuilder(params); pb.directory(workingDir); for (Entry<String, String> e : env.entrySet()) { pb.environment().put(e.getKey(), e.getValue()); } Process p = pb.redirectErrorStream(true).start(); p.getOutputStream().close(); BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = in.readLine()) != null) { buffer.append(line).append("\n"); } int retval = p.waitFor(); if (retval != 0) { throw new CommandException( "The command execution returned " + retval + " as return value... !\n" + buffer); } return buffer.toString(); } catch (IOException e) { throw new CommandException(e); } catch (InterruptedException e) { throw new CommandException("It seems the process was killed", e); } }
From source file:io.jmnarloch.cd.go.plugin.gradle.GradleTaskExecutor.java
/** * Executes the actual Gradle build./* w w w . ja v a 2 s. c o m*/ * * @param builder the process builder * @param console the log output * @return the process return value * @throws IOException if any error occurs during I/O operation * @throws InterruptedException if any error occurs during process execution */ private static int execute(ProcessBuilder builder, JobConsoleLogger console) throws IOException, InterruptedException { Process process = null; try { process = builder.start(); console.readOutputOf(process.getInputStream()); console.readErrorOf(process.getErrorStream()); return process.waitFor(); } finally { if (process != null) { process.destroy(); } } }
From source file:Main.java
private static String executeTop() { Process p = null; BufferedReader in = null;//from w w w . ja va2 s . co m String returnString = null; try { p = Runtime.getRuntime().exec("top -n 1"); in = new BufferedReader(new InputStreamReader(p.getInputStream())); while (returnString == null || returnString.contentEquals("")) { returnString = in.readLine(); } } catch (IOException e) { Log.e("executeTop", "error in getting first line of top"); e.printStackTrace(); } finally { try { in.close(); p.destroy(); } catch (IOException e) { Log.e("executeTop", "error in closing and destroying top process"); e.printStackTrace(); } } return returnString; }
From source file:BluemixUtils.java
private static String executeCmdCommand(String command) throws IOException { System.out.println("Execute " + command); ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", command); // builder.redirectErrorStream(true); Process p = builder.start(); BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream())); StringBuilder result = new StringBuilder(); String line;/* www. ja v a2 s .co m*/ while (true) { line = r.readLine(); if (line == null) { break; } result.append(line); System.out.println(line); } return result.toString(); }
From source file:Main.java
public static String getMac() { String mac = null;//w ww . jav a 2s .c o m String macSerial = null; String str = ""; try { Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address"); // Process pp = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address"); InputStreamReader ir = new InputStreamReader(pp.getInputStream()); LineNumberReader input = new LineNumberReader(ir); for (; null != str;) { str = input.readLine(); if (str != null) { macSerial = str.trim(); break; } } } catch (IOException ex) { ex.printStackTrace(); } if (macSerial != null) { macSerial = macSerial.replace(":", ""); macSerial = macSerial.replace("a", "A"); macSerial = macSerial.replace("b", "B"); macSerial = macSerial.replace("c", "C"); macSerial = macSerial.replace("d", "D"); macSerial = macSerial.replace("e", "E"); mac = macSerial.replace("f", "F"); } return mac; }
From source file:com.yahoo.storm.yarn.TestIntegration.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private static int execute(List<String> cmd) throws InterruptedException, IOException { LOG.info(Joiner.on(" ").join(cmd)); ProcessBuilder pb = new ProcessBuilder(cmd); Map env = pb.environment();// w w w . j a v a2s. co m env.putAll(System.getenv()); env.put(Environment.PATH.name(), "bin:" + storm_home + File.separator + "bin:" + env.get(Environment.PATH.name())); String yarn_conf_dir = yarn_site_xml.getParent().toString(); env.put("STORM_YARN_CONF_DIR", yarn_conf_dir); List<URL> logback_xmls = Utils.findResources("logback.xml"); if (logback_xmls != null && logback_xmls.size() >= 1) { String logback_xml = logback_xmls.get(0).getFile(); LOG.debug("logback_xml:" + yarn_conf_dir + File.separator + "logback.xml"); FileUtils.copyFile(new File(logback_xml), new File(yarn_conf_dir + File.separator + "logback.xml")); } List<URL> log4j_properties = Utils.findResources("log4j.properties"); if (log4j_properties != null && log4j_properties.size() >= 1) { String log4j_properties_file = log4j_properties.get(0).getFile(); LOG.debug("log4j_properties_file:" + yarn_conf_dir + File.separator + "log4j.properties"); FileUtils.copyFile(new File(log4j_properties_file), new File(yarn_conf_dir + File.separator + "log4j.properties")); } Process proc = pb.start(); Util.redirectStreamAsync(proc.getInputStream(), System.out); Util.redirectStreamAsync(proc.getErrorStream(), System.err); int status = proc.waitFor(); return status; }