List of usage examples for java.lang ProcessBuilder start
public Process start() throws IOException
From source file:Main.java
public static String runCommand(String[] command, String workdirectory) { String result = ""; Log.d("AppUtil.class", "#" + command); try {/* ww w. jav a2 s. co m*/ ProcessBuilder builder = new ProcessBuilder(command); // set working directory if (workdirectory != null) { builder.directory(new File(workdirectory)); } builder.redirectErrorStream(true); Process process = builder.start(); InputStream in = process.getInputStream(); byte[] buffer = new byte[1024]; while (in.read(buffer) != -1) { String str = new String(buffer); result = result + str; } in.close(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:brooklyn.util.io.FileUtil.java
private static int exec(List<String> cmds, OutputStream out, OutputStream err) { StreamGobbler errgobbler = null;/*ww w.j ava 2 s. co 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:de.uni.bremen.monty.moco.Main.java
private static File buildExecutable(String outputFileName, String inputFileName, boolean compileOnly, String llvmCode) throws IOException, InterruptedException { File outputFile = null;/* www. j a va 2s . c o m*/ if (outputFileName != null) { outputFile = new File(outputFileName); } else if (inputFileName != null) { outputFile = new File(FilenameUtils.removeExtension(inputFileName)); } else if (compileOnly) { outputFile = File.createTempFile("output", null, null); outputFile.deleteOnExit(); } else { outputFile = new File("output"); } ProcessBuilder llcProcessBuilder = new ProcessBuilder("llc", "-O=2"); Process llcProcess = llcProcessBuilder.start(); PrintStream llcInput = new PrintStream(llcProcess.getOutputStream()); llcInput.print(llvmCode); llcInput.close(); ProcessBuilder ccProcessBuilder = new ProcessBuilder("cc", "-x", "assembler", "-o", outputFile.getAbsolutePath(), "-"); Process ccProcess = ccProcessBuilder.start(); IOUtils.copy(llcProcess.getInputStream(), ccProcess.getOutputStream()); ccProcess.getOutputStream().close(); System.err.print(IOUtils.toString(llcProcess.getErrorStream())); System.err.print(IOUtils.toString(ccProcess.getErrorStream())); return outputFile; }
From source file:edu.cwru.jpdg.Dotty.java
public static void graphviz(String name, String graph) { byte[] bytes = graph.getBytes(); try {//w w w . j av a2 s . c o m Path p = Paths.get("./reports/" + name + ".dot"); Files.write(p, bytes); } catch (IOException e) { System.out.println(graph); } try { ProcessBuilder pb = new ProcessBuilder("dot", "-Tpng", "-o", "reports/" + name + ".png"); pb.redirectError(ProcessBuilder.Redirect.INHERIT); Process p = pb.start(); OutputStream stdin = p.getOutputStream(); stdin.write(bytes); stdin.close(); String line; BufferedReader stdout = new BufferedReader(new InputStreamReader(p.getInputStream())); List<String> stdout_lines = new ArrayList<String>(); line = stdout.readLine(); while (line != null) { stdout_lines.add(line); line = stdout.readLine(); } if (p.waitFor() != 0) { throw new RuntimeException("javac failed"); } } catch (InterruptedException e) { throw new RuntimeException(e.getMessage()); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } }
From source file:com.limegroup.gnutella.util.Launcher.java
/** * Opens the default web browser on windows, passing it the specified * url./*from w w w .j a v a 2 s . com*/ * * @param url the url to open in the browser * @return the error code of the native call, -1 if the call failed * for any reason */ private static int openURLWindows(String url) throws IOException { //Windows like escaping of '&' character when passed as part of a parameter //& -> ^& url = url.replace("&", "^&"); String[] command = new String[] { "cmd.exe", "/c", "start", url }; ProcessBuilder pb = new ProcessBuilder(command); pb.start(); return 0; }
From source file:Main.java
private static void openWindows(File f) { try {/*from w ww .j av a 2 s . c o m*/ ProcessBuilder builder = new ProcessBuilder(); List<String> lst = new ArrayList<String>(); lst.add("rundll32"); lst.add("url.dll,FileProtocolHandler"); lst.add(f.getAbsolutePath()); builder.command(lst); builder.start(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:ee.ria.xroad.confproxy.util.ConfProxyHelper.java
/** * Helper method for running the configuration client script. * @param pb the configuration client script process builder * @throws Exception if errors occur when running the configuration client *//*ww w. jav a 2s. c om*/ private static void runConfClient(final ProcessBuilder pb) throws Exception { int exitCode = -1; try { Process process = pb.start(); exitCode = process.waitFor(); } catch (IOException e) { log.error("IOException", e); exitCode = 2; throw e; } catch (Exception e) { log.error("Undetermined ConfigurationClient exitCode", e); //undetermined ConfigurationClient exitCode, fail in 'finally' throw e; } switch (exitCode) { case SUCCESS: break; case ERROR_CODE_CANNOT_DOWNLOAD_CONF: throw new Exception("configuration-client error (exit code " + exitCode + "), download failed"); case ERROR_CODE_EXPIRED_CONF: throw new Exception( "configuration-client error (exit code " + exitCode + "), configuration is outdated"); case ERROR_CODE_INVALID_SIGNATURE_VALUE: throw new Exception( "configuration-client error (exit code " + exitCode + "), configuration is incorrect"); case ERROR_CODE_INTERNAL: throw new Exception("configuration-client error (exit code " + exitCode + ")"); default: throw new Exception("Failed to download GlobalConf " + "(configuration-client exit code " + exitCode + "), " + "make sure configuration-client is" + "installed correctly"); } }
From source file:com.googlecode.promnetpp.research.main.CompareOutputMain.java
private static void doSeedRun(int seed) throws IOException, InterruptedException { System.out.println("Running with seed " + seed); String pattern = "int random = <INSERT_SEED_HERE>;"; int start = sourceCode.indexOf(pattern); int end = start + pattern.length(); String line = sourceCode.substring(start, end); line = line.replace("<INSERT_SEED_HERE>", Integer.toString(seed)); String sourceCodeWithSeed = sourceCode.replace(pattern, line); File tempFile = new File("temp.pml"); FileUtils.writeStringToFile(tempFile, sourceCodeWithSeed); //Create a "project" folder String fileNameWithoutExtension = fileName.split("[.]")[0]; File folder = new File("test1-" + fileNameWithoutExtension); if (folder.exists()) { FileUtils.deleteDirectory(folder); }/*from ww w . j a va2s .co m*/ folder.mkdir(); //Copy temp.pml to our new folder FileUtils.copyFileToDirectory(tempFile, folder); //Simulate the model using Spin List<String> spinCommand = new ArrayList<String>(); spinCommand.add(GeneralData.spinHome + "/spin"); spinCommand.add("-u1000000"); spinCommand.add("temp.pml"); ProcessBuilder processBuilder = new ProcessBuilder(spinCommand); processBuilder.directory(folder); processBuilder.redirectOutput(new File(folder, "spin-" + seed + ".txt")); Process process = processBuilder.start(); process.waitFor(); //Translate via PROMNeT++ List<String> PROMNeTppCommand = new ArrayList<String>(); PROMNeTppCommand.add("java"); PROMNeTppCommand.add("-enableassertions"); PROMNeTppCommand.add("-jar"); PROMNeTppCommand.add("\"" + GeneralData.getJARFilePath() + "\""); PROMNeTppCommand.add("temp.pml"); processBuilder = new ProcessBuilder(PROMNeTppCommand); processBuilder.directory(folder); processBuilder.environment().put("PROMNETPP_HOME", GeneralData.PROMNeTppHome); process = processBuilder.start(); process.waitFor(); //Run opp_makemake FileUtils.copyFileToDirectory(new File("opp_makemake.bat"), folder); List<String> makemakeCommand = new ArrayList<String>(); if (Utilities.operatingSystemType.equals("windows")) { makemakeCommand.add("cmd"); makemakeCommand.add("/c"); makemakeCommand.add("opp_makemake.bat"); } else { throw new RuntimeException("Support for Linux/OS X not implemented" + " here yet."); } processBuilder = new ProcessBuilder(makemakeCommand); processBuilder.directory(folder); process = processBuilder.start(); process.waitFor(); //Run make FileUtils.copyFileToDirectory(new File("opp_make.bat"), folder); List<String> makeCommand = new ArrayList<String>(); if (Utilities.operatingSystemType.equals("windows")) { makeCommand.add("cmd"); makeCommand.add("/c"); makeCommand.add("opp_make.bat"); } else { throw new RuntimeException("Support for Linux/OS X not implemented" + " here yet."); } processBuilder = new ProcessBuilder(makeCommand); processBuilder.directory(folder); process = processBuilder.start(); process.waitFor(); System.out.println(Utilities.getStreamAsString(process.getInputStream())); System.exit(1); }
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;/*from w w w . ja va2 s. c o m*/ while (true) { line = r.readLine(); if (line == null) { break; } result.append(line); System.out.println(line); } return result.toString(); }
From source file:com.cedarsoft.io.LinkUtils.java
/** * Creates a link.// w w w .j a v a 2s .com * Returns true if the link has been created, false if the link (with the same link source) still exists. * * @param linkTarget the link source * @param linkFile the link file * @param symbolic whether to create a symbolic link * @return whether the link has been created (returns false if the link still existed) * * @throws IOException if something went wrong */ public static boolean createLink(@Nonnull File linkTarget, @Nonnull File linkFile, boolean symbolic) throws IOException { if (linkFile.exists()) { //Maybe the hard link still exists - we just don't know, so throw an exception if (!symbolic) { throw new IOException("link still exists " + linkFile.getAbsolutePath()); } if (linkFile.getCanonicalFile().equals(linkTarget.getCanonicalFile())) { //still exists - that is ok, since it points to the same directory return false; } else { //Other target throw new IOException("A link still exists at <" + linkFile.getAbsolutePath() + "> but with different target: <" + linkTarget.getCanonicalPath() + "> exected <" + linkFile.getCanonicalPath() + ">"); } } List<String> args = new ArrayList<String>(); args.add("ln"); if (symbolic) { args.add("-s"); } args.add(linkTarget.getPath()); args.add(linkFile.getAbsolutePath()); ProcessBuilder builder = new ProcessBuilder(args); Process process = builder.start(); try { int result = process.waitFor(); if (result != 0) { throw new IOException("Creation of link failed: " + IOUtils.toString(process.getErrorStream())); } } catch (InterruptedException e) { throw new RuntimeException(e); } return true; }