List of usage examples for java.lang Process getErrorStream
public abstract InputStream getErrorStream();
From source file:com.adguard.compiler.PackageUtils.java
private static void execute(String... commands) throws IOException, InterruptedException { ProcessBuilder pb = new ProcessBuilder(commands); Process p = pb.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line;// ww w . ja v a2 s. c om while ((line = reader.readLine()) != null) { log.debug(line); } p.waitFor(); if (p.exitValue() != 0) { reader = new BufferedReader(new InputStreamReader(p.getErrorStream())); while ((line = reader.readLine()) != null) { log.error(line); } throw new IOException("Command " + ArrayUtils.toString(commands) + " not success"); } }
From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java
private static void decompressUtil(Path archiveFilePath, Path workingDir, boolean stripParent) throws Exception { try {/* w ww . ja v a2s .c om*/ MutableList<String> tarArgs = Lists.mutable.of(UNIX_TAR, "xvzf", archiveFilePath.toFile().getAbsolutePath()); if (stripParent) { tarArgs = tarArgs.with("--strip").with("1"); } Process process = new ProcessBuilder(tarArgs.toList()).directory(workingDir.toFile()).start(); int exitCode = process.waitFor(); if (exitCode != 0) { logStdout(process.getInputStream()); logStdErr(process.getErrorStream()); throw new Exception("Failed to decompress"); } } catch (Exception e) { throw new Exception("Failed to decompress", e); } }
From source file:com.goldmansachs.kata2go.tools.utils.TarGz.java
public static void compress(Path sourceDirectoryPath, Path archiveFilePath) throws Exception { try {//w w w. j ava 2 s . c om ImmutableList<String> tarArgs = Lists.immutable.of(UNIX_TAR, "cvzf", archiveFilePath.toFile().getAbsolutePath(), "."); Process process = new ProcessBuilder(tarArgs.toList()).directory(sourceDirectoryPath.toFile()).start(); int exitCode = process.waitFor(); if (exitCode != 0) { logStdout(process.getInputStream()); logStdErr(process.getErrorStream()); throw new Exception("Failed to compress"); } } catch (Exception e) { throw new Exception("Failed to compress", e); } }
From source file:io.github.jeddict.jcode.parser.ejs.EJSUtil.java
public static void executeCommand(FileObject workingFolder, ProgressHandler handler, String... command) { try {//from w w w .j a va 2 s .c o m ProcessBuilder pb = new ProcessBuilder(command); // Map<String, String> env = pb.environment(); // If you want clean environment, call env.clear() first // env.put("VAR1", "myValue"); // env.remove("OTHERVAR"); // env.put("VAR2", env.get("VAR1") + "suffix"); pb.directory(FileUtil.toFile(workingFolder)); Process proc = pb.start(); BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(proc.getErrorStream())); // read the output from the command String s; while ((s = stdInput.readLine()) != null) { handler.append(Console.wrap(s, FG_BLUE)); } // read any errors from the attempted command while ((s = stdError.readLine()) != null) { handler.append(Console.wrap(s, FG_DARK_RED)); } } catch (IOException ex) { Exceptions.printStackTrace(ex); } }
From source file:Main.java
static void execPrivileged(final String[] cmd_array) throws Exception { try {/*from w ww. ja v a2 s . c o m*/ Process process = AccessController.doPrivileged(new PrivilegedExceptionAction<Process>() { public Process run() throws Exception { return Runtime.getRuntime().exec(cmd_array); } }); // Close unused streams to make sure the child process won't hang process.getInputStream().close(); process.getOutputStream().close(); process.getErrorStream().close(); } catch (PrivilegedActionException e) { throw (Exception) e.getCause(); } }
From source file:org.spring.data.gemfire.AbstractGemFireIntegrationTest.java
protected static ServerLauncher startGemFireServer(final long waitTimeout, final String cacheXmlPathname, final Properties gemfireProperties) throws IOException { String gemfireMemberName = gemfireProperties.getProperty(DistributionConfig.NAME_NAME); String serverId = DATE_FORMAT.format(Calendar.getInstance().getTime()); gemfireMemberName = String.format("%1$s-%2$s", (StringUtils.hasText(gemfireMemberName) ? gemfireMemberName : ""), serverId); File serverWorkingDirectory = FileSystemUtils.createFile(gemfireMemberName.toLowerCase()); Assert.isTrue(FileSystemUtils.createDirectory(serverWorkingDirectory), String.format("Failed to create working directory (%1$s) in which the GemFire Server will run!", serverWorkingDirectory)); ServerLauncher serverLauncher = buildServerLauncher(cacheXmlPathname, gemfireProperties, serverId, DEFAULT_SERVER_PORT, serverWorkingDirectory); List<String> serverCommandLine = buildServerCommandLine(serverLauncher); System.out.printf("Starting GemFire Server in (%1$s)...%n", serverWorkingDirectory); Process serverProcess = ProcessUtils.startProcess( serverCommandLine.toArray(new String[serverCommandLine.size()]), serverWorkingDirectory); readProcessStream(serverId, "ERROR", serverProcess.getErrorStream()); readProcessStream(serverId, "OUT", serverProcess.getInputStream()); waitOnServer(waitTimeout, serverProcess, serverWorkingDirectory); return serverLauncher; }
From source file:de.helmholtz_muenchen.ibis.utils.threads.ExecuteThread.java
/** * Catch STDERR of process and return it as StringBuffer * @param p the process//from w w w . j av a 2s .c o m * @return StringBuffer containing the STDERR of the given process * @throws IOException */ private static StringBuffer getLogEntryStdErr(Process p) throws IOException { String s = null; StringBuffer nodeEntry = new StringBuffer(60); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); while ((s = stdError.readLine()) != null) { nodeEntry.append(s + "\n"); } return nodeEntry; }
From source file:com.moss.posixfifosockets.PosixFifoSocket.java
public static void createFifo(File path) throws IOException { if (path.exists()) { throw new IOException("File already exists: " + path.getAbsolutePath()); }// w w w . j av a 2 s .c o m if (!path.getParentFile().exists()) { throw new FileNotFoundException(path.getParent()); } try { if (path.exists()) { throw new RuntimeException("Path really does exist: " + path.getAbsolutePath()); } final Process p = Runtime.getRuntime().exec("mkfifo " + path.getAbsolutePath()); int result = p.waitFor(); if (result != 0) { String stdOut = read(p.getInputStream()); String stdErr = read(p.getErrorStream()); throw new IOException("Error creating fifo at " + path.getAbsolutePath() + ": Received error code " + result + ", STDOUT: " + stdOut + ", STDERR: " + stdErr); } else if (!path.exists()) { throw new RuntimeException("mkfifo didn't do its job: " + path.getAbsolutePath()); } } catch (InterruptedException e) { throw new RuntimeException(e); } }
From source file:com.liferay.blade.samples.test.BladeCLIUtil.java
public static String execute(File workingDir, String... bladeArgs) throws Exception { String bladeCLIJarPath = getLatestBladeCLIJar(); List<String> command = new ArrayList<>(); command.add("java"); command.add("-jar"); command.add(bladeCLIJarPath);/* ww w . ja v a2s. c om*/ for (String arg : bladeArgs) { command.add(arg); } Process process = new ProcessBuilder(command.toArray(new String[0])).directory(workingDir).start(); process.waitFor(); InputStream stream = process.getInputStream(); String output = new String(IO.read(stream)); InputStream errorStream = process.getErrorStream(); List<String> errorList = new ArrayList<>(); if (errorStream != null) { errorList.add(new String(IO.read(errorStream))); } List<String> filteredErrorList = new ArrayList<>(); for (String string : errorList) { if (!string.isEmpty() && !string.contains("Picked up JAVA_TOOL_OPTIONS:")) { filteredErrorList.add(string); } } assertTrue(filteredErrorList.toString(), filteredErrorList.isEmpty()); output = StringUtil.toLowerCase(output); return output; }
From source file:com.cedarsoft.io.LinkUtils.java
/** * Creates a link.// w w w . j av a 2 s. 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; }