java.lang.ProcessBuilder.Redirect Java Examples
The following examples show how to use
java.lang.ProcessBuilder.Redirect.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: OrionProcessRunner.java From orion with Apache License 2.0 | 7 votes |
public void start(final String processName) throws IOException { final Config config = Config.load(this.configFilePath); final Path workPath = config.workDir(); final List<String> params = Lists.newArrayList(); params.add(executableLocation()); params.add(configFilePath.toString()); final ProcessBuilder processBuilder = new ProcessBuilder(params) .directory(new File(System.getProperty("user.dir"))) .redirectErrorStream(true) .redirectInput(Redirect.INHERIT); try { final Process process = processBuilder.start(); outputProcessorExecutor.submit(() -> printOutput(processName, process)); processes.put(processName, process); } catch (final IOException e) { LOG.error("Error starting Orion process", e); } loadPortsFile(workPath); }
Example #2
Source File: VMTargetStarter.java From gravel with Apache License 2.0 | 7 votes |
public Process startSecondJVM(Class<?> mainClassToStart) throws IOException { String separator = System.getProperty("file.separator"); String classpath = System.getProperty("java.class.path"); String path = System.getProperty("java.home") + separator + "bin" + separator + "java"; ProcessBuilder processBuilder = new ProcessBuilder(path, "-Xdebug", "-Xrunjdwp:transport=dt_socket,address=" + debugPort + ",server=y,suspend=y", "-cp", classpath, mainClassToStart.getCanonicalName()); processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); final Process process = processBuilder.start(); Thread closeChildThread = new Thread() { public void run() { process.destroy(); } }; Runtime.getRuntime().addShutdownHook(closeChildThread); return process; }
Example #3
Source File: Edition069_Audio_Capture.java From appiumpro with Apache License 2.0 | 7 votes |
public void run() { ArrayList<String> cmd = new ArrayList<>(); cmd.add("ffmpeg"); // binary should be on path cmd.add("-y"); // always overwrite files cmd.add("-f"); // format cmd.add("avfoundation"); // apple's system audio---something else for windows cmd.add("-i"); // input cmd.add(":" + deviceId); // device id returned by ffmpeg list cmd.add(captureFile.getAbsolutePath()); ProcessBuilder pb = new ProcessBuilder(cmd); pb.redirectErrorStream(true); pb.redirectOutput(Redirect.PIPE); StringJoiner out = new StringJoiner("\n"); try { proc = pb.start(); try (BufferedReader reader = new BufferedReader( new InputStreamReader(proc.getInputStream()))) { reader.lines().forEach(out::add); } proc.waitFor(); } catch (IOException | InterruptedException ign) {} System.out.println("FFMpeg output was: " + out.toString()); }
Example #4
Source File: MainTest.java From google-java-format with Apache License 2.0 | 6 votes |
@Test public void exitIfChangedStdin() throws Exception { Path path = testFolder.newFile("Test.java").toPath(); Files.write(path, "class Test {\n}\n".getBytes(UTF_8)); Process process = new ProcessBuilder( ImmutableList.of( Paths.get(System.getProperty("java.home")).resolve("bin/java").toString(), "-cp", System.getProperty("java.class.path"), Main.class.getName(), "-n", "--set-exit-if-changed", "-")) .redirectInput(path.toFile()) .redirectError(Redirect.PIPE) .redirectOutput(Redirect.PIPE) .start(); process.waitFor(); String err = new String(ByteStreams.toByteArray(process.getErrorStream()), UTF_8); String out = new String(ByteStreams.toByteArray(process.getInputStream()), UTF_8); assertThat(err).isEmpty(); assertThat(out).isEqualTo("<stdin>" + System.lineSeparator()); assertThat(process.exitValue()).isEqualTo(1); }
Example #5
Source File: BtrfsService.java From btrbck with GNU General Public License v3.0 | 6 votes |
public void receive(Path destinationPath, Consumer<OutputStream> callback) { try { Process process = processBuilder("btrfs", "receive", "-e", destinationPath.toAbsolutePath().toString()) .redirectOutput(Redirect.PIPE).start(); callback.consume(process.getOutputStream()); process.getOutputStream().close(); int exitValue = process.waitFor(); if (exitValue != 0) { throw new IOException("exit code: " + exitValue); } } catch (IOException | InterruptedException e) { throw new RuntimeException( "Error while receiving snapshot sub volume in " + destinationPath, e); } }
Example #6
Source File: JavaSubprocessFactory.java From android-test with Apache License 2.0 | 6 votes |
/** * Returns a {@link ProcessBuilder.Redirect} appropriate for the parameters. If a file redirected * to exists, deletes the file before redirecting to it. */ private Redirect getRedirect(StreamAction action, File file) { switch (action) { case DISCARD: return Redirect.to(new File("/dev/null")); case REDIRECT: // We need to use Redirect.appendTo() here, because on older Linux kernels writes are // otherwise not atomic and might result in lost log messages: // https://lkml.org/lkml/2014/3/3/308 if (file.exists()) { file.delete(); } return Redirect.appendTo(file); case STREAM: return Redirect.PIPE; default: throw new IllegalStateException(); } }
Example #7
Source File: ElasticSearchInstaller.java From ES-Fastloader with Apache License 2.0 | 6 votes |
private void installPlugin(File pluginManager, InstallationDescription.Plugin plugin) throws IOException, InterruptedException { logger.info("> " + pluginManager + " install " + plugin.getExpression()); ProcessBuilder builder = new ProcessBuilder(); builder.redirectOutput(Redirect.PIPE); builder.redirectErrorStream(true); builder.command(prepareInstallCommand(pluginManager, plugin)); Process process = builder.start(); BufferedReader bReader = new BufferedReader(new InputStreamReader(process.getInputStream(), UTF_8)); String line; while ((line = bReader.readLine()) != null) { logger.info(String.format("Plugin install %s: %s", plugin, line)); } if (process.waitFor() != 0) { throw new EmbeddedElasticsearchStartupException("Unable to install plugin: " + plugin); } }
Example #8
Source File: ProcessBuilderUnitTest.java From tutorials with MIT License | 6 votes |
@Test public void givenProcessBuilder_whenRedirectStandardOutput_thenSuccessAppending() throws IOException, InterruptedException { ProcessBuilder processBuilder = new ProcessBuilder("java", "-version"); File log = tempFolder.newFile("java-version-append.log"); processBuilder.redirectErrorStream(true); processBuilder.redirectOutput(Redirect.appendTo(log)); Process process = processBuilder.start(); assertEquals("If redirected output, should be -1 ", -1, process.getInputStream() .read()); int exitCode = process.waitFor(); assertEquals("No errors should be detected", 0, exitCode); List<String> lines = Files.lines(log.toPath()) .collect(Collectors.toList()); assertThat("Results should not be empty", lines, is(not(empty()))); assertThat("Results should contain java version: ", lines, hasItem(containsString("java version"))); }
Example #9
Source File: IntervalClassification.java From sequence-mining with GNU General Public License v3.0 | 6 votes |
/** Run shell script with command line arguments */ public static void runScript(final String cmd[], final File outFile) { try { final ProcessBuilder pb = new ProcessBuilder(cmd); if (outFile != null) pb.redirectOutput(outFile); else pb.redirectOutput(Redirect.INHERIT); pb.redirectError(Redirect.INHERIT); final Process process = pb.start(); process.waitFor(); process.destroy(); } catch (final Exception e) { e.printStackTrace(); } }
Example #10
Source File: LocalAppDeployer.java From spring-cloud-deployer-local with Apache License 2.0 | 6 votes |
private void start(ProcessBuilder builder, Path workDir, boolean deleteOnExit) throws IOException { String workDirPath = workDir.toFile().getAbsolutePath(); this.stdout = Files.createFile(Paths.get(workDirPath, "stdout_" + instanceNumber + ".log")).toFile(); this.attributes.put("stdout", stdout.getAbsolutePath()); this.stderr = Files.createFile(Paths.get(workDirPath, "stderr_" + instanceNumber + ".log")).toFile(); this.attributes.put("stderr", stderr.getAbsolutePath()); if (deleteOnExit) { this.stdout.deleteOnExit(); this.stderr.deleteOnExit(); } builder.redirectOutput(Redirect.to(this.stdout)); builder.redirectError(Redirect.to(this.stderr)); this.start(builder, workDir); }
Example #11
Source File: SubProcessKernel.java From beam with Apache License 2.0 | 6 votes |
private ProcessBuilder prepareBuilder( ProcessBuilder builder, SubProcessCommandLineArgs commands, SubProcessIOFiles outPutFiles) throws IllegalStateException { // Check we are not over the max size of command line parameters if (getTotalCommandBytes(commands) > MAX_SIZE_COMMAND_LINE_ARGS) { throw new IllegalStateException("Command is over 2MB in size"); } appendExecutablePath(builder); // Add the result file path to the builder at position 1, 0 is reserved for the process itself builder.command().add(1, outPutFiles.resultFile.toString()); // Shift commands by 2 ordinal positions and load into the builder for (SubProcessCommandLineArgs.Command s : commands.getParameters()) { builder.command().add(s.ordinalPosition + 2, s.value); } builder.redirectError(Redirect.appendTo(outPutFiles.errFile.toFile())); builder.redirectOutput(Redirect.appendTo(outPutFiles.outFile.toFile())); return builder; }
Example #12
Source File: PipeProcessesAndAwaitCompletion.java From demo-java-x with MIT License | 6 votes |
public static void main(String[] args) throws IOException, InterruptedException { ProcessBuilder ls = new ProcessBuilder() .command("tree", "-i") .directory(Paths.get("/home/nipa").toFile()); ProcessBuilder grepPdf = new ProcessBuilder() .command("grep", "pdf") .redirectOutput(Redirect.INHERIT); List<Process> lsThenGrep = ProcessBuilder.startPipeline(asList(ls, grepPdf)); System.out.println("Started processes..."); CompletableFuture[] lsThenGrepFutures = lsThenGrep.stream() // onExit returns a CompletableFuture<Process> .map(Process::onExit) .map(processFuture -> processFuture.thenAccept( process -> System.out.println("Process " + process.pid() + " finished."))) .toArray(CompletableFuture[]::new); // wait until all processes are finished CompletableFuture .allOf(lsThenGrepFutures) .join(); }
Example #13
Source File: SshService.java From btrbck with GNU General Public License v3.0 | 6 votes |
private ProcessBuilder processBuilder(SshTarget target, List<String> commands) { // construct command LinkedList<String> list = new LinkedList<String>(); list.add("ssh"); // add port if (target.getPort() != null) { list.add("-p"); list.add(target.getPort().toString()); } // add host list.add(target.getHost()); // add commands list.addAll(commands); return new ProcessBuilder().redirectError(Redirect.INHERIT).command( list); }
Example #14
Source File: Bundler.java From gcs with Mozilla Public License 2.0 | 6 votes |
private static List<String> runCmd(List<String> args) { List<String> lines = new ArrayList<>(); ProcessBuilder builder = new ProcessBuilder(args); builder.redirectOutput(Redirect.PIPE).redirectErrorStream(true); try { boolean hadMsg = false; Process process = builder.start(); try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) { String line = in.readLine(); while (line != null) { lines.add(line); line = in.readLine(); } } } catch (IOException exception) { System.out.println(); exception.printStackTrace(System.err); System.exit(1); } return lines; }
Example #15
Source File: MainTest.java From google-java-format with Apache License 2.0 | 6 votes |
@Test public void exitIfChangedFiles() throws Exception { Path path = testFolder.newFile("Test.java").toPath(); Files.write(path, "class Test {\n}\n".getBytes(UTF_8)); Process process = new ProcessBuilder( ImmutableList.of( Paths.get(System.getProperty("java.home")).resolve("bin/java").toString(), "-cp", System.getProperty("java.class.path"), Main.class.getName(), "-n", "--set-exit-if-changed", path.toAbsolutePath().toString())) .redirectError(Redirect.PIPE) .redirectOutput(Redirect.PIPE) .start(); process.waitFor(); String err = new String(ByteStreams.toByteArray(process.getErrorStream()), UTF_8); String out = new String(ByteStreams.toByteArray(process.getInputStream()), UTF_8); assertThat(err).isEmpty(); assertThat(out).isEqualTo(path.toAbsolutePath().toString() + System.lineSeparator()); assertThat(process.exitValue()).isEqualTo(1); }
Example #16
Source File: JavaSubprocessFactory.java From bazel with Apache License 2.0 | 6 votes |
/** * Returns a {@link java.lang.ProcessBuilder.Redirect} appropriate for the parameters. If a file * redirected to exists, deletes the file before redirecting to it. */ private Redirect getRedirect(StreamAction action, File file) { switch (action) { case DISCARD: return Redirect.to(new File("/dev/null")); case REDIRECT: // We need to use Redirect.appendTo() here, because on older Linux kernels writes are // otherwise not atomic and might result in lost log messages: // https://lkml.org/lkml/2014/3/3/308 if (file.exists()) { file.delete(); } return Redirect.appendTo(file); case STREAM: return Redirect.PIPE; default: throw new IllegalStateException(); } }
Example #17
Source File: SubProcessKernel.java From deployment-examples with MIT License | 6 votes |
private ProcessBuilder prepareBuilder( ProcessBuilder builder, SubProcessCommandLineArgs commands, SubProcessIOFiles outPutFiles) throws IllegalStateException { // Check we are not over the max size of command line parameters if (getTotalCommandBytes(commands) > MAX_SIZE_COMMAND_LINE_ARGS) { throw new IllegalStateException("Command is over 2MB in size"); } appendExecutablePath(builder); // Add the result file path to the builder at position 1, 0 is reserved for the process itself builder.command().add(1, outPutFiles.resultFile.toString()); // Shift commands by 2 ordinal positions and load into the builder for (SubProcessCommandLineArgs.Command s : commands.getParameters()) { builder.command().add(s.ordinalPosition + 2, s.value); } builder.redirectError(Redirect.appendTo(outPutFiles.errFile.toFile())); builder.redirectOutput(Redirect.appendTo(outPutFiles.outFile.toFile())); return builder; }
Example #18
Source File: SimpleProcessManager.java From Singularity with Apache License 2.0 | 5 votes |
public List<String> runCommandWithOutput( final List<String> command, final Set<Integer> acceptableExitCodes ) throws InterruptedException, ProcessFailedException { return runCommand(command, Redirect.PIPE, acceptableExitCodes); }
Example #19
Source File: BtrfsService.java From btrbck with GNU General Public License v3.0 | 5 votes |
public void send(SendFileSpec sendFile, Consumer<InputStream> callback) { try { LinkedList<String> args = new LinkedList<>(); args.addAll(Arrays.asList("btrfs", "send")); { boolean firstSource = true; for (Snapshot s : sendFile.cloneSources) { if (firstSource) { args.add("-p"); firstSource = false; } else { args.add("-c"); } args.add(s.getSnapshotDir().toAbsolutePath().toString()); } } args.add(sendFile.target.getSnapshotDir().toAbsolutePath() .toString()); Process process = processBuilder(args) .redirectOutput(Redirect.PIPE).start(); callback.consume(process.getInputStream()); int exitValue = process.waitFor(); if (exitValue != 0) { throw new IOException("exit code: " + exitValue); } } catch (IOException | InterruptedException e) { throw new RuntimeException( "Error while sending snapshot sub volume in " + sendFile, e); } }
Example #20
Source File: WindowsIT.java From java-certificate-authority with Apache License 2.0 | 5 votes |
/** * {@code certreq -accept cert.cer} * * @throws IOException * @throws InterruptedException */ private void acceptCert() throws IOException, InterruptedException { final Process process = new ProcessBuilder("certreq", "-accept", "cert.cer") .redirectError(Redirect.INHERIT) .redirectOutput(Redirect.INHERIT) .start(); process.waitFor(); }
Example #21
Source File: SimpleProcessManager.java From Singularity with Apache License 2.0 | 5 votes |
public void runCommand( final List<String> command, final Set<Integer> acceptableExitCodes ) throws InterruptedException, ProcessFailedException { runCommand(command, Redirect.INHERIT, acceptableExitCodes); }
Example #22
Source File: WindowsIT.java From java-certificate-authority with Apache License 2.0 | 5 votes |
/** * {@code certutil -enterprise -addstore ROOT ca.cer} * * @throws IOException * @throws InterruptedException */ private void installTrustedCaCert() throws IOException, InterruptedException { final Process process = new ProcessBuilder("certutil", "-enterprise", "-addstore", "ROOT", "ca.cer") .redirectError(Redirect.INHERIT) .redirectOutput(Redirect.INHERIT) .start(); process.waitFor(); }
Example #23
Source File: WindowsIT.java From java-certificate-authority with Apache License 2.0 | 5 votes |
/** * {@code netsh http add sslcert ipport=0.0.0.0:443 certhash=... appid=...} * * @param certHash * @param appId * @throws IOException * @throws InterruptedException */ private void configureSsl(final String certHash, final String appId) throws IOException, InterruptedException { final String certhashParam = "certhash=" + certHash; final String appidParam = "appid={" + appId + "}"; final Process process = new ProcessBuilder("netsh", "http", "add", "sslcert", "ipport=0.0.0.0:443", "certstorename=MY", certhashParam, appidParam) .redirectError(Redirect.INHERIT) .redirectOutput(Redirect.INHERIT) .start(); process.waitFor(); }
Example #24
Source File: OpenFilesUtils.java From flink-crawler with Apache License 2.0 | 5 votes |
public List<String> logOpenFiles(String directory) { // System.out.println("Finding open files in " + directory); // String[] cmd = new String[] {"lsof", "+D", directory}; List<String> result = new ArrayList<>(); String[] cmd = new String[] { "lsof" }; ProcessBuilder builder = new ProcessBuilder(cmd); builder.redirectOutput(Redirect.PIPE); try { closeStreams(); Process p = builder.start(); List<String> files = org.apache.commons.io.IOUtils.readLines(p.getInputStream()); for (String file : files) { // System.out.println(file); if (file.contains(directory)) { result.add(file); } } return result; } catch (IOException e) { throw new RuntimeException("Error generating list of open files", e); } finally { openStreams(); } }
Example #25
Source File: SingularityExecutorTaskLogManager.java From Singularity with Apache License 2.0 | 5 votes |
private void copyLogTail() { if (configuration.getTailLogLinesToSave() <= 0) { return; } final Path tailOfLogPath = taskDefinition.getServiceFinishedTailLogPath(); if (Files.exists(tailOfLogPath)) { log.debug("{} already existed, skipping tail", tailOfLogPath); return; } final List<String> cmd = ImmutableList.of( "tail", "-n", Integer.toString(configuration.getTailLogLinesToSave()), taskDefinition.getServiceLogOut() ); try { new SimpleProcessManager(log).runCommand(cmd, Redirect.to(tailOfLogPath.toFile())); } catch (Throwable t) { log.error( "Failed saving tail of log {} to {}", taskDefinition.getServiceLogOut(), taskDefinition.getServiceFinishedTailLogPath(), t ); } }
Example #26
Source File: ProcessFactory.java From r2cloud with Apache License 2.0 | 5 votes |
public ProcessWrapper create(String commandLine, Redirect redirectError, boolean inheritIO) throws IOException { LOG.info("started with arguments: {}", commandLine); ProcessBuilder processBuilder = new ProcessBuilder(SPACE.split(commandLine)); if (redirectError != null) { processBuilder.redirectError(redirectError); } if (inheritIO) { processBuilder.inheritIO(); } return new ProcessWrapperImpl(processBuilder.start()); }
Example #27
Source File: RtlSdrReader.java From r2cloud with Apache License 2.0 | 5 votes |
@Override public IQData start() throws InterruptedException { File rawFile = new File(config.getTempDirectory(), req.getSatelliteId() + "-" + req.getId() + ".raw.gz"); Long startTimeMillis = null; Long endTimeMillis = null; try { Integer ppm = config.getInteger("ppm.current"); if (ppm == null) { ppm = 0; } startTimeMillis = System.currentTimeMillis(); rtlSdr = factory.create(config.getProperty("satellites.rtlsdrwrapper.path") + " -rtl " + config.getProperty("satellites.rtlsdr.path") + " -f " + req.getActualFrequency() + " -s " + req.getInputSampleRate() + " -g " + config.getProperty("satellites.rtlsdr.gain") + " -p " + ppm + " -o " + rawFile.getAbsolutePath(), Redirect.INHERIT, false); int responseCode = rtlSdr.waitFor(); // rtl_sdr should be killed by the reaper process // all other codes are invalid. even 0 if (responseCode != 143) { LOG.error("[{}] invalid response code rtl_sdr: {}", req.getId(), responseCode); Util.deleteQuietly(rawFile); } else { LOG.info("[{}] rtl_sdr stopped: {}", req.getId(), responseCode); } } catch (IOException e) { LOG.error("[{}] unable to run", req.getId(), e); } finally { endTimeMillis = System.currentTimeMillis(); } IQData result = new IQData(); result.setActualStart(startTimeMillis); result.setActualEnd(endTimeMillis); if (rawFile.exists()) { result.setDataFile(rawFile); } return result; }
Example #28
Source File: BtrfsService.java From btrbck with GNU General Public License v3.0 | 5 votes |
private ProcessBuilder processBuilder(LinkedList<String> list) { if (useStrace()) { list.addFirst("-ff"); list.addFirst("strace." + MDC.get("id") + ".log"); list.addFirst("-o"); list.addFirst("strace"); } if (useSudo()) { list.addFirst("sudo"); } log.debug("created process builder: " + list); return new ProcessBuilder().redirectError(Redirect.INHERIT) .redirectOutput(Redirect.INHERIT).command(list); }
Example #29
Source File: ProcessRunner.java From neoscada with Eclipse Public License 1.0 | 5 votes |
public ProcessRunner ( final ProcessBuilder processBuilder ) { this.processBuilder = processBuilder; processBuilder.redirectError ( Redirect.INHERIT ); processBuilder.redirectOutput ( Redirect.INHERIT ); processBuilder.redirectInput ( Redirect.INHERIT ); }
Example #30
Source File: OpenSslIT.java From java-certificate-authority with Apache License 2.0 | 5 votes |
/** * {@code openssl req -nodes -newkey rsa:2048 -keyout private.key -out CSR.csr -subj "/CN=localhost"} * * @throws IOException * @throws InterruptedException */ private void generateCsr() throws IOException, InterruptedException { final Process process = new ProcessBuilder("openssl", "req", "-nodes", "-newkey", "rsa:2048", "-keyout", "private.key", "-out", "CSR.csr", "-subj", "/CN=localhost") .redirectError(Redirect.INHERIT) .redirectOutput(Redirect.INHERIT) .start(); process.waitFor(); }