List of usage examples for java.lang Process getInputStream
public abstract InputStream getInputStream();
From source file:com.mqm.frame.util.license.CollectMacAddress.java
/** * Mac?/* w ww .j a va2s .com*/ * * @return Mac? */ public static List<String> getMacAddress() { List<String> macAddressList = new ArrayList<String>(); String os = InternationalizationUtil.toLowerCase(System.getProperty("os.name")); if (os != null && os.startsWith("windows")) { BufferedReader br = null; Process p = null; try { String command = "cmd.exe /c ipconfig /all"; p = Runtime.getRuntime().exec(command); br = new BufferedReader(new InputStreamReader(p.getInputStream())); String line; while ((line = br.readLine()) != null) { if (line.indexOf("Physical Address") > 0) { int index = line.indexOf(":"); index += 2; macAddressList.add(line.substring(index).trim()); } } return macAddressList; } catch (Exception e) { log.info("", e); } finally { if (br != null) { try { br.close(); } catch (IOException e) { log.error("", e); } } if (p != null) { //? p.destroy(); } } } return macAddressList; }
From source file:com.o2d.pkayjava.editor.utils.Overlap2DUtils.java
private static String getMyDocumentsLocation() { String myDocuments = null;//from w w w . j a v a2 s . c om try { if (SystemUtils.IS_OS_MAC || SystemUtils.IS_OS_MAC_OSX) { myDocuments = System.getProperty("user.home") + File.separator + "Documents"; } if (SystemUtils.IS_OS_WINDOWS) { Process p = Runtime.getRuntime().exec( "reg query \"HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders\" /v personal"); p.waitFor(); InputStream in = p.getInputStream(); byte[] b = new byte[in.available()]; in.read(b); in.close(); myDocuments = new String(b); myDocuments = myDocuments.split("\\s\\s+")[4]; } if (SystemUtils.IS_OS_LINUX) { myDocuments = System.getProperty("user.home") + File.separator + "Documents"; } } catch (Throwable t) { t.printStackTrace(); } return myDocuments; }
From source file:com.impetus.ankush.agent.utils.CommandExecutor.java
/** * Method to execute command arrays./*www. j a v a2s . c om*/ * * @param command * @return * @throws IOException * @throws InterruptedException */ public static Result executeCommand(String... command) throws IOException, InterruptedException { Result rs = new Result(); Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(command); StreamWrapper error = new StreamWrapper(proc.getErrorStream()); StreamWrapper output = new StreamWrapper(proc.getInputStream()); error.start(); output.start(); error.join(SLEEP_TIME); output.join(SLEEP_TIME); proc.waitFor(); rs.setExitVal(proc.exitValue()); rs.setOutput(output.getMessage()); rs.setError(error.getMessage()); proc.destroy(); return rs; }
From source file:com.impetus.ankush.agent.utils.CommandExecutor.java
/** * Method executeCommand.//from w w w . j ava 2 s .c o m * * @param command * String * @return Result * @throws IOException * Signals that an I/O exception has occurred. * @throws InterruptedException * the interrupted exception */ public static Result executeCommand(String command) throws IOException, InterruptedException { Result rs = new Result(); Runtime rt = Runtime.getRuntime(); Process proc = rt.exec(command); StreamWrapper error = new StreamWrapper(proc.getErrorStream()); StreamWrapper output = new StreamWrapper(proc.getInputStream()); error.start(); output.start(); error.join(SLEEP_TIME); output.join(SLEEP_TIME); proc.waitFor(); rs.setExitVal(proc.exitValue()); rs.setOutput(output.getMessage()); rs.setError(error.getMessage()); proc.destroy(); return rs; }
From source file:SystemKit.java
/** * Returns the first line of the result of a shell command. * Taken from UUID./*from w w w . j a v a2 s . c o m*/ * * @param commands the commands to run * @return the first line of the command * @throws IOException * * @since 3.3.3 */ static String getFirstLineOfCommand(String[] commands) throws IOException { Process p = null; BufferedReader reader = null; try { p = Runtime.getRuntime().exec(commands); reader = new BufferedReader(new InputStreamReader(p.getInputStream()), 128); return reader.readLine(); } finally { if (p != null) { if (reader != null) { try { reader.close(); } catch (IOException ex) { } } try { p.getErrorStream().close(); } catch (IOException ex) { } try { p.getOutputStream().close(); } catch (IOException ex) { } p.destroy(); } } }
From source file:abs.backend.erlang.ErlangTestDriver.java
public static boolean checkErlang() { if (SemanticTests.checkProg("erl")) { // http://stackoverflow.com/a/9561398/60462 ProcessBuilder pb = new ProcessBuilder("erl", "-eval", "erlang:display(erlang:system_info(otp_release)), halt().", "-noshell"); try {/* w w w .j a v a 2 s.c o m*/ Process p = pb.start(); InputStreamReader r = new InputStreamReader(p.getInputStream()); BufferedReader b = new BufferedReader(r); Assert.assertEquals(0, p.waitFor()); String version = b.readLine(); java.util.regex.Pattern pat = java.util.regex.Pattern.compile("\"(\\d*).*"); Matcher m = pat.matcher(version); Assert.assertTrue("Could not identify Erlang version: " + version, m.matches()); String v = m.group(1); Assume.assumeTrue("Need Erlang R17 or better.", Integer.parseInt(v) >= 17); } catch (IOException e) { return false; } catch (InterruptedException e) { return false; } } return true; }
From source file:com.streamsets.datacollector.util.ClusterUtil.java
public static void killYarnApp(String testName) throws Exception { // TODO - remove this hack // We dont know app id, but yarn creates its data dir under $HOME/target/TESTNAME, so kill the process by // grep for the yarn testname String killCmd = signalCommand(testName, "SIGKILL"); LOG.info("Signal kill command to yarn app " + killCmd); String[] killCommand = new String[] { "/usr/bin/env", "bash", "-c", killCmd }; Process p = Runtime.getRuntime().exec(killCommand); p.waitFor();/*from w w w.j a v a 2s . c om*/ BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = ""; LOG.info("Process output is "); while ((line = reader.readLine()) != null) { LOG.debug(line + "\n"); } }
From source file:eu.cloud4soa.c4sgitservice.utils.Util.java
public static synchronized int replaceBlockWithSedForAuthorizedKeys(String filename, String pubkeyid) { int retvalue = 1; //1=ERROR 0=OK try {// w w w . j a v a 2 s . c o m String sedblock = createSedBlockForAuthorizedKeys(pubkeyid); System.out.println(" sed -e " + sedblock + " " + filename); String[] command = new String[] { "sed", "-i", // -i is for replace // -e is used for piping sedblock, filename }; System.out.println(command.toString()); Process child = Runtime.getRuntime().exec(command); child.waitFor(); //Get the input stream and read from it InputStream in = child.getInputStream(); int c; while ((c = in.read()) != -1) { System.out.print((char) c); } in.close(); retvalue = child.exitValue(); System.out.println("Replace file returned: " + retvalue); } catch (Exception ex) { ex.printStackTrace(); } return retvalue; }
From source file:eu.cloud4soa.c4sgitservice.utils.Util.java
public static synchronized int replaceBlockWithSedForProxyGit(String filename, String tag) { int retvalue = 1; //1=ERROR 0=OK try {/* w w w .j a v a2s.c om*/ String sedblock = createSedBlockForProxyGit(tag); System.out.println(" sed -e " + sedblock + " " + filename); String[] command = new String[] { "sed", "-i", // -i is for replace // -e is used for piping sedblock, filename }; System.out.println(command.toString()); Process child = Runtime.getRuntime().exec(command); child.waitFor(); //Get the input stream and read from it InputStream in = child.getInputStream(); int c; while ((c = in.read()) != -1) { System.out.print((char) c); } in.close(); retvalue = child.exitValue(); System.out.println("Replace file returned: " + retvalue); } catch (Exception ex) { ex.printStackTrace(); } return retvalue; }
From source file:MD5.java
public static String getRecoveryMD5() { String MD5string = ""; String recoveryFilename = "/dev/mtd/mtd1"; try {//from w ww . java 2s.co m Process p = Runtime.getRuntime().exec("su"); OutputStream os = p.getOutputStream(); os.write(("md5sum " + recoveryFilename).getBytes()); os.flush(); os.close(); InputStream is = p.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String str = br.readLine(); MD5string = str.split(" ")[0].trim(); is.close(); br.close(); p.destroy(); } catch (Exception e) { System.out.println(e); return null; } System.out.println(MD5string); return MD5string; }