List of usage examples for java.lang Process waitFor
public abstract int waitFor() throws InterruptedException;
From source file:jeplus.RadianceWinTools.java
/** * Call Rtrace to run the simulation//w ww .j a v a 2 s .co m * @param config Radiance Configuration * @param WorkDir The working directory where the input files are stored and the output files to be generated * @param args * @param model * @param in * @param out * @param err * @param process * @return the result code represents the state of execution steps. >=0 means successful */ public static int runRtrace(RadianceConfig config, String WorkDir, String args, String model, String in, String out, String err, ProcessWrapper process) { int ExitValue = -99; try { StringBuilder buf = new StringBuilder(config.getResolvedRadianceBinDir()); buf.append(File.separator).append("rtrace"); List<String> command = new ArrayList<>(); command.add(buf.toString()); String[] arglist = args.split("\\s+"); command.addAll(Arrays.asList(arglist)); command.add(model); ProcessBuilder builder = new ProcessBuilder(command); builder.directory(new File(WorkDir)); builder.environment().put("RAYPATH", "." + File.pathSeparator + config.getResolvedRadianceLibDir()); builder.redirectError(new File(WorkDir + File.separator + err)); builder.redirectOutput(new File(WorkDir + File.separator + out)); builder.redirectInput(new File(WorkDir + File.separator + in)); Process proc = builder.start(); if (process != null) { process.setWrappedProc(proc); } ExitValue = proc.waitFor(); } catch (IOException | InterruptedException ex) { logger.error("Error occoured when executing Rtrace", ex); } // Return Radiance exit value return ExitValue; }
From source file:net.pms.util.ProcessUtil.java
public static String run(int[] expectedExitCodes, String... cmd) { try {//from w w w .j a v a 2 s . c o m 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:org.zenoss.zep.dao.impl.DaoUtils.java
public static int executeCommand(String command) { String response = ""; ProcessBuilder pb = new ProcessBuilder("bash", "-c", command); pb.redirectErrorStream(true);/* w w w . java2 s . co m*/ int shellExitStatus = -1; InputStream shellIn = null; try { Process shell = pb.start(); // To capture output from the shell shellIn = shell.getInputStream(); // Wait for the shell to finish and get the return code shellExitStatus = shell.waitFor(); if (shellExitStatus != 0) { response = DaoUtils.convertStreamToStr(shellIn); logger.error("Error (return code: " + shellExitStatus + ") from \"" + command + "\": \nOutput: " + response); } shellIn.close(); } catch (IOException e) { logger.error("Error occured while executing Linux command. Error Description: " + e.getMessage()); } catch (InterruptedException e) { logger.error("Error occured while executing Linux command. Error Description: " + e.getMessage()); } return shellExitStatus; }
From source file:jeplus.RadianceWinTools.java
/** * Call DaySim gen_dc to run the simulation * @param config Radiance Configuration/*ww w .ja v a 2s .c o m*/ * @param WorkDir The working directory where the input files are stored and the output files to be generated * @param model * @param in * @param out * @param err * @param process * @return the result code represents the state of execution steps. >=0 means successful */ public static int runGen_DC(RadianceConfig config, String WorkDir, String model, String in, String out, String err, ProcessWrapper process) { int ExitValue = -99; // Manipulate header file HashMap<String, String> props = new HashMap<>(); // props.put("project_name", ""); props.put("project_directory", "./"); props.put("bin_directory", config.getResolvedDaySimBinDir()); props.put("tmp_directory", "./"); props.put("Template_File", config.getResolvedDaySimBinDir() + "../template/"); props.put("sensor_file", in); try { FileUtils.moveFile(new File(WorkDir + File.separator + model), new File(WorkDir + File.separator + model + ".ori")); } catch (IOException ex) { logger.error("Error renaming header file to " + WorkDir + File.separator + model + ".ori", ex); } DaySimModel.updateHeaderFile(WorkDir + File.separator + model + ".ori", WorkDir + File.separator + model, props); // Run command try { StringBuilder buf = new StringBuilder(config.getResolvedDaySimBinDir()); buf.append(File.separator).append("gen_dc"); List<String> command = new ArrayList<>(); command.add(buf.toString()); command.add(model); ProcessBuilder builder = new ProcessBuilder(command); builder.directory(new File(WorkDir)); builder.environment().put("RAYPATH", "." + File.pathSeparator + config.getResolvedDaySimLibDir()); builder.redirectOutput(new File(WorkDir + File.separator + out)); if (err == null || out.equals(err)) { builder.redirectErrorStream(true); } else { builder.redirectError(new File(WorkDir + File.separator + err)); } if (in != null) { builder.redirectInput(new File(WorkDir + File.separator + in)); } Process proc = builder.start(); if (process != null) { process.setWrappedProc(proc); } ExitValue = proc.waitFor(); } catch (IOException | InterruptedException ex) { logger.error("Error occoured when executing DaySim gen_dc", ex); } // Return Radiance exit value return ExitValue; }
From source file:com.sinpo.xnfc.nfc.Util.java
/** * ??? Root??(ROOT??)//from w w w . j a v a 2 s . c om * * @return ?/??Root?? */ public static boolean upgradeRootPermission(String pkgCodePath) { Process process = null; DataOutputStream os = null; try { String cmd = "chmod 777 " + pkgCodePath; process = Runtime.getRuntime().exec("su"); //?root?? os = new DataOutputStream(process.getOutputStream()); os.writeBytes(cmd + "\n"); os.writeBytes("exit\n"); os.flush(); process.waitFor(); } catch (Exception e) { return false; } finally { try { if (os != null) { os.close(); } process.destroy(); } catch (Exception e) { } } return true; }
From source file:brooklyn.util.io.FileUtil.java
private static int exec(List<String> cmds, OutputStream out, OutputStream err) { StreamGobbler errgobbler = null;//from www . j a va 2 s . c o m StreamGobbler outgobbler = null; ProcessBuilder pb = new ProcessBuilder(cmds); try { Process p = pb.start(); if (out != null) { InputStream outstream = p.getInputStream(); outgobbler = new StreamGobbler(outstream, out, (Logger) null); outgobbler.start(); } if (err != null) { InputStream errstream = p.getErrorStream(); errgobbler = new StreamGobbler(errstream, err, (Logger) null); errgobbler.start(); } int result = p.waitFor(); if (outgobbler != null) outgobbler.blockUntilFinished(); if (errgobbler != null) errgobbler.blockUntilFinished(); return result; } catch (Exception e) { throw Exceptions.propagate(e); } finally { Streams.closeQuietly(outgobbler); Streams.closeQuietly(errgobbler); } }
From source file:io.hops.hopsworks.common.security.PKIUtils.java
public static String getSubjectFromCSR(String csr) throws IOException, InterruptedException { File csrFile = File.createTempFile(System.getProperty("java.io.tmpdir"), ".csr"); FileUtils.writeStringToFile(csrFile, csr); List<String> cmds = new ArrayList<>(); //openssl req -in certs-dir/hops-site-certs/csr.pem -noout -subject cmds.add("openssl"); cmds.add("req"); cmds.add("-in"); cmds.add(csrFile.getAbsolutePath()); cmds.add("-noout"); cmds.add("-subject"); StringBuilder sb = new StringBuilder("/usr/bin/ "); for (String s : cmds) { sb.append(s).append(" "); }/*from w w w . j av a 2s. c o m*/ logger.info(sb.toString()); Process process = new ProcessBuilder(cmds).directory(new File("/usr/bin/")).redirectErrorStream(true) .start(); BufferedReader br = new BufferedReader( new InputStreamReader(process.getInputStream(), Charset.forName("UTF8"))); String line; StringBuilder lines = new StringBuilder(""); while ((line = br.readLine()) != null) { logger.info(line); lines.append(line); } process.waitFor(); int exitValue = process.exitValue(); if (exitValue != 0) { throw new RuntimeException("Failed to get subject. Exit value: " + exitValue); } return lines.toString(); }
From source file:io.hops.hopsworks.common.security.PKIUtils.java
public static String getSerialNumberFromCert(String cert) throws IOException, InterruptedException { File csrFile = File.createTempFile(System.getProperty("java.io.tmpdir"), ".pem"); FileUtils.writeStringToFile(csrFile, cert); List<String> cmds = new ArrayList<>(); //openssl x509 -in certs-dir/hops-site-certs/pub.pem -noout -serial cmds.add("openssl"); cmds.add("x509"); cmds.add("-in"); cmds.add(csrFile.getAbsolutePath()); cmds.add("-noout"); cmds.add("-serial"); StringBuilder sb = new StringBuilder("/usr/bin/ "); for (String s : cmds) { sb.append(s).append(" "); }/*from ww w . j a v a2s. c o m*/ logger.info(sb.toString()); Process process = new ProcessBuilder(cmds).directory(new File("/usr/bin/")).redirectErrorStream(true) .start(); BufferedReader br = new BufferedReader( new InputStreamReader(process.getInputStream(), Charset.forName("UTF8"))); String line; StringBuilder lines = new StringBuilder(""); while ((line = br.readLine()) != null) { logger.info(line); lines.append(line); } process.waitFor(); int exitValue = process.exitValue(); if (exitValue != 0) { throw new RuntimeException("Failed to get Serial Number. Exit value: " + exitValue); } return lines.toString(); }
From source file:Main.java
public static boolean symlink(File inFile, File outFile) { int exitCode = -1; try {//from w ww. j a v a 2 s .c om Process sh = Runtime.getRuntime().exec("sh"); OutputStream out = sh.getOutputStream(); String command = "/system/bin/ln -s " + inFile + " " + outFile + "\nexit\n"; out.write(command.getBytes("ASCII")); final char buf[] = new char[40]; InputStreamReader reader = new InputStreamReader(sh.getInputStream()); while (reader.read(buf) != -1) throw new IOException("stdout: " + new String(buf)); reader = new InputStreamReader(sh.getErrorStream()); while (reader.read(buf) != -1) throw new IOException("stderr: " + new String(buf)); exitCode = sh.waitFor(); } catch (IOException e) { e.printStackTrace(); return false; } catch (InterruptedException e) { e.printStackTrace(); return false; } return exitCode == 0; }
From source file:io.hops.hopsworks.common.security.PKIUtils.java
public static String verifyCertificate(Settings settings, String cert, boolean intermediate) throws IOException, InterruptedException { File certFile = File.createTempFile(System.getProperty("java.io.tmpdir"), ".pem"); FileUtils.writeStringToFile(certFile, cert); String certDir = intermediate ? settings.getIntermediateCaDir() : settings.getCaDir(); String crlFile = intermediate ? certDir + "/crl/intermediate.crl.pem" : certDir + "/crl/ca.crl.pem"; String pubCert = intermediate ? "/intermediate/certs/intermediate.cert.pem " : "/certs/ca.cert.pem "; //update the crl createCRL(settings, intermediate);/*from w ww . j a va 2 s . c om*/ logger.info("Checking certificate..."); List<String> cmds = new ArrayList<>(); cmds.add("openssl"); cmds.add("verify"); cmds.add("-crl_check"); cmds.add("-CAfile"); cmds.add("<(cat " + certDir + pubCert + crlFile + ")"); cmds.add(certFile.getAbsolutePath()); StringBuilder sb = new StringBuilder("/usr/bin/"); for (String s : cmds) { sb.append(s).append(" "); } logger.info(sb.toString()); Process process = new ProcessBuilder(cmds).directory(new File("/usr/bin/")).redirectErrorStream(true) .start(); BufferedReader br = new BufferedReader( new InputStreamReader(process.getInputStream(), Charset.forName("UTF8"))); String line; StringBuilder lines = new StringBuilder(""); while ((line = br.readLine()) != null) { logger.info(line); lines.append(line); } process.waitFor(); int exitValue = process.exitValue(); if (exitValue != 0) { throw new RuntimeException("Failed cert check. Exit value: " + exitValue); } logger.info("Done cert check."); return lines.toString(); }