List of usage examples for java.lang Process getInputStream
public abstract InputStream getInputStream();
From source file:ee.ria.xroad.proxy.ProxyMain.java
private static void readProxyVersion() { try {// w ww.j ava2s . co m String cmd; if (Files.exists(Paths.get("/etc/redhat-release"))) { cmd = "rpm -q --queryformat '%{VERSION}-%{RELEASE}' xroad-proxy"; } else { cmd = "dpkg-query -f '${Version}' -W xroad-proxy"; } Process p = Runtime.getRuntime().exec(cmd); p.waitFor(); version = IOUtils.toString(p.getInputStream()).replace("'", ""); if (StringUtils.isBlank(version)) { version = "unknown"; log.warn("Unable to read proxy version: {}", IOUtils.toString(p.getErrorStream())); } } catch (Exception ex) { version = "unknown"; log.warn("Unable to read proxy version", ex); } }
From source file:net.pms.util.ProcessUtil.java
public static String run(int[] expectedExitCodes, String... cmd) { try {/*from www .j a v a 2 s .c om*/ ProcessBuilder pb = new ProcessBuilder(cmd); pb.redirectErrorStream(true); Process p = pb.start(); StringBuilder output; try (BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()))) { String line; output = new StringBuilder(); while ((line = br.readLine()) != null) { output.append(line).append("\n"); } } p.waitFor(); boolean expected = false; if (expectedExitCodes != null) { for (int expectedCode : expectedExitCodes) { if (expectedCode == p.exitValue()) { expected = true; break; } } } if (!expected) { LOGGER.debug("Warning: command {} returned {}", Arrays.toString(cmd), p.exitValue()); } return output.toString(); } catch (Exception e) { LOGGER.error("Error running command " + Arrays.toString(cmd), e); } return ""; }
From source file:com.kylinolap.metadata.tool.HiveSourceTableMgmt.java
/** * @param hiveCommd/*w ww . j ava2s . co m*/ */ private static String callGenerateCommand(String hiveCommd) throws IOException { // Get out put path String tempDir = System.getProperty("java.io.tmpdir"); logger.info("OS current temporary directory is " + tempDir); if (StringUtils.isEmpty(tempDir)) { tempDir = "/tmp"; } String[] cmd = new String[2]; String osName = System.getProperty("os.name"); if (osName.startsWith("Windows")) { cmd[0] = "cmd.exe"; cmd[1] = "/C"; } else { cmd[0] = "/bin/bash"; cmd[1] = "-c"; } // hive command output // String hiveOutputPath = tempDir + File.separator + // "tmp_kylin_output"; String hiveOutputPath = File.createTempFile("HiveOutput", null).getAbsolutePath(); // Metadata output File dir = File.createTempFile("meta", null); dir.delete(); dir.mkdir(); String tableMetaOutcomeDir = dir.getAbsolutePath(); ProcessBuilder pb = null; if (osName.startsWith("Windows")) { pb = new ProcessBuilder(cmd[0], cmd[1], "ssh root@sandbox 'hive -e \"" + hiveCommd + "\"' > " + hiveOutputPath); } else { pb = new ProcessBuilder(cmd[0], cmd[1], "hive -e \"" + hiveCommd + "\" > " + hiveOutputPath); } // Run hive pb.directory(new File(tempDir)); pb.redirectErrorStream(true); Process p = pb.start(); InputStream is = p.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = null; try { br = new BufferedReader(isr); String line = null; logger.info("Execute : " + pb.command().get(0)); while ((line = br.readLine()) != null) { logger.info(line); } } finally { if (null != br) { br.close(); } } logger.info("Hive execution completed!"); HiveSourceTableMgmt rssMgmt = new HiveSourceTableMgmt(); rssMgmt.extractTableDescFromFile(hiveOutputPath, tableMetaOutcomeDir); return tableMetaOutcomeDir; }
From source file:eu.stratosphere.nephele.instance.HardwareDescriptionFactory.java
/** * Returns the size of the physical memory in bytes on a Mac OS-based * operating system//from w w w.j ava 2 s . c o m * * @return the size of the physical memory in bytes or <code>-1</code> if * the size could not be determined */ private static long getSizeOfPhysicalMemoryForMac() { BufferedReader bi = null; try { Process proc = Runtime.getRuntime().exec("sysctl hw.memsize"); bi = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line; while ((line = bi.readLine()) != null) { if (line.startsWith("hw.memsize")) { long memsize = Long.parseLong(line.split(":")[1].trim()); bi.close(); proc.destroy(); return memsize; } } } catch (Exception e) { LOG.error(e); return -1; } finally { if (bi != null) { try { bi.close(); } catch (IOException ioe) { } } } return -1; }
From source file:ai.h2o.servicebuilder.Util.java
/** * Run command cmd in separate process in directory * * @param directory run in this directory * @param cmd command to run// w w w .jav a2 s. co m * @param errorMessage error message if process didn't finish with exit value 0 * @return stdout combined with stderr * @throws Exception */ public static String runCmd(File directory, List<String> cmd, String errorMessage) throws Exception { ProcessBuilder pb = new ProcessBuilder(cmd); pb.directory(directory); pb.redirectErrorStream(true); // error sent to output stream Process p = pb.start(); // get output stream to string String s; StringBuilder sb = new StringBuilder(); BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((s = stdout.readLine()) != null) { logger.info(s); sb.append(s); sb.append('\n'); } String sbs = sb.toString(); int exitValue = p.waitFor(); if (exitValue != 0) throw new Exception(errorMessage + " exit value " + exitValue + " " + sbs); return sbs; }
From source file:de.helmholtz_muenchen.ibis.utils.threads.ExecuteThread.java
/** * Catch STDOUT of process and return it as StringBuffer * @param p the process//from ww w .java2 s . c o m * @return StringBuffer containing the STDOUT of the given process * @throws IOException */ private static StringBuffer getLogEntryStdOut(Process p) throws IOException { String s = null; StringBuffer nodeEntry = new StringBuffer(60); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); while ((s = stdInput.readLine()) != null) { nodeEntry.append(s + "\n"); } return nodeEntry; }
From source file:eu.stratosphere.nephele.instance.HardwareDescriptionFactory.java
/** * Returns the size of the physical memory in bytes on FreeBSD. * /*from w w w . j a v a2s . c om*/ * @return the size of the physical memory in bytes or <code>-1</code> if * the size could not be determined */ private static long getSizeOfPhysicalMemoryForFreeBSD() { BufferedReader bi = null; try { Process proc = Runtime.getRuntime().exec("sysctl hw.physmem"); bi = new BufferedReader(new InputStreamReader(proc.getInputStream())); String line; while ((line = bi.readLine()) != null) { if (line.startsWith("hw.physmem")) { long memsize = Long.parseLong(line.split(":")[1].trim()); bi.close(); proc.destroy(); return memsize; } } LOG.error("Cannot determine the size of the physical memory using 'sysctl hw.physmem'."); return -1; } catch (Exception e) { LOG.error( "Cannot determine the size of the physical memory using 'sysctl hw.physmem': " + e.getMessage(), e); return -1; } finally { if (bi != null) { try { bi.close(); } catch (IOException ioe) { } } } }
From source file:Main.java
public static String runScript(String script) { String sRet;//from ww w . j av a 2 s. com try { final Process m_process = Runtime.getRuntime().exec(script); final StringBuilder sbread = new StringBuilder(); Thread tout = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getInputStream()), 8192); String ls_1; try { while ((ls_1 = bufferedReader.readLine()) != null) { sbread.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); tout.start(); final StringBuilder sberr = new StringBuilder(); Thread terr = new Thread(new Runnable() { public void run() { BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(m_process.getErrorStream()), 8192); String ls_1; try { while ((ls_1 = bufferedReader.readLine()) != null) { sberr.append(ls_1).append("\n"); } } catch (IOException e) { e.printStackTrace(); } finally { try { bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } } }); terr.start(); m_process.waitFor(); while (tout.isAlive()) { Thread.sleep(50); } if (terr.isAlive()) terr.interrupt(); String stdout = sbread.toString(); String stderr = sberr.toString(); sRet = stdout + stderr; } catch (Exception e) { e.printStackTrace(); return null; } return sRet; }
From source file:org.spring.data.gemfire.AbstractGemFireIntegrationTest.java
protected static OutputStream startSpringGemFireServer(final long waitTimeout, final String... springConfigLocations) throws IOException { String serverId = DATE_FORMAT.format(Calendar.getInstance().getTime()); File serverWorkingDirectory = FileSystemUtils.createFile("server-".concat(serverId)); Assert.isTrue(FileSystemUtils.createDirectory(serverWorkingDirectory), String.format( "Failed to create working directory (%1$s) in which the Spring-based GemFire Server will run!", serverWorkingDirectory));//from ww w .j a v a 2s .c om String[] serverCommandLine = buildServerCommandLine(springConfigLocations); System.out.printf("Starting Spring GemFire Server in (%1$s)...%n", serverWorkingDirectory); Process serverProcess = ProcessUtils.startProcess(serverCommandLine, serverWorkingDirectory); readProcessStream(serverId, "ERROR", serverProcess.getErrorStream()); readProcessStream(serverId, "OUT", serverProcess.getInputStream()); ProcessUtils.registerProcessShutdownHook(serverProcess, "Spring GemFire Server", serverWorkingDirectory); waitOnServer(waitTimeout, serverProcess, serverWorkingDirectory); return serverProcess.getOutputStream(); }
From source file:javadepchecker.Main.java
private static Collection<String> getPackageJars(String pkg) { ArrayList<String> jars = new ArrayList<String>(); try {//from ww w .j ava 2 s . c om Process p = Runtime.getRuntime().exec("java-config -p " + pkg); p.waitFor(); BufferedReader in; in = new BufferedReader(new InputStreamReader(p.getInputStream())); String output = in.readLine(); if (output != null/* package somehow missing*/ && !output.trim().equals("")) { for (String jar : output.split(":")) { jars.add(jar); } } } catch (InterruptedException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } return jars; }