List of usage examples for java.lang ProcessBuilder start
public Process start() throws IOException
From source file:com.apress.prospringintegration.customadapters.outbound.ShellMessageWritingMessageEndpoint.java
protected int writeToShellCommand(String[] cmds, String msg) { try {/* w w w .j a v a 2 s.co m*/ ProcessBuilder processBuilder = new ProcessBuilder(Arrays.asList(cmds)); Process proc = processBuilder.start(); Writer streamWriter = null; try { streamWriter = new OutputStreamWriter(proc.getOutputStream()); streamWriter.write(msg); } finally { IOUtils.closeQuietly(streamWriter); } int retVal = proc.waitFor(); if (retVal != 0) { throw new RuntimeException("couldn't write message to 'write'"); } return retVal; } catch (Throwable th) { throw new RuntimeException(th); } }
From source file:com.frostwire.gui.bittorrent.SlideDownload.java
private void executeSlide(Slide slide) { List<String> command = new ArrayList<String>(); command.add(getSaveLocation().getAbsolutePath()); if (slide.executeParameters != null) { command.addAll(Arrays.asList(slide.executeParameters.split(" "))); }// w w w.j a v a 2 s.c o m BufferedReader br = null; ProcessBuilder pb = new ProcessBuilder(command); try { Process p = pb.start(); //consume all output to avoid deadlock in some verisons of windows br = new BufferedReader(new InputStreamReader(p.getInputStream())); while (br.readLine() != null) { } } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(br); } }
From source file:com.scooter1556.sms.server.io.StreamProcess.java
private void run(String[] command) throws IOException { LogService.getInstance().addLogEntry(LogService.Level.DEBUG, CLASS_NAME, StringUtils.join(command, " "), null);//from w w w . j a v a 2 s. co m ProcessBuilder processBuilder = new ProcessBuilder(command); process = processBuilder.start(); InputStream input = process.getInputStream(); OutputStream output = response.getOutputStream(); new NullStream(process.getErrorStream()).start(); // Buffer byte[] buffer = new byte[BUFFER_SIZE]; int length; // Write stream to output while ((length = input.read(buffer)) != -1) { output.write(buffer, 0, length); bytesTransferred += length; } }
From source file:com.hazelcast.simulator.boot.SimulatorInstaller.java
private void decompress(File archive) throws InterruptedException, IOException { ProcessBuilder pb = new ProcessBuilder("tar", "-xpf", archive.getAbsolutePath(), "-C", getUserHome().getAbsolutePath()); pb.start().waitFor(); }
From source file:org.anarres.qemu.image.QEmuImage.java
@Nonnull public QEmuImageInfo query() throws IOException { ProcessBuilder builder = new ProcessBuilder("qemu-img", "info", "--output=json", file.getAbsolutePath()); Process process = builder.start(); byte[] data = ByteStreams.toByteArray(process.getInputStream()); ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(data, QEmuImageInfo.class); }
From source file:io.hakbot.providers.shell.ShellProvider.java
public boolean process(Job job) { try (InputStream inputStream = process.getInputStream(); InputStream errorStream = process.getErrorStream()) { final ProcessBuilder pb = new ProcessBuilder(command.split(" ")); process = pb.start(); final int exitCode = process.waitFor(); final byte[] stdout = IOUtils.toByteArray(inputStream); final byte[] stderr = IOUtils.toByteArray(errorStream); if (LOGGER.isDebugEnabled()) { LOGGER.debug("STDOUT:"); LOGGER.debug(new String(stdout)); LOGGER.debug("STDERR:"); LOGGER.debug(new String(stderr)); }//from w w w . j ava2 s . com if (exitCode == 0) { addArtifact(job, JobArtifact.Type.PROVIDER_RESULT, JobArtifact.MimeType.PLAIN_TEXT.value(), stdout, "Console-STDOUT-" + job.getUuid() + ".txt"); } else { if (stdout.length == 0) { addArtifact(job, JobArtifact.Type.PROVIDER_RESULT, JobArtifact.MimeType.PLAIN_TEXT.value(), stderr, "Console-STDERR-" + job.getUuid() + ".txt"); } else { addArtifact(job, JobArtifact.Type.PROVIDER_RESULT, JobArtifact.MimeType.PLAIN_TEXT.value(), stdout, "Console-STDOUT-" + job.getUuid() + ".txt"); } throw new JobException(exitCode); } return true; } catch (IOException | InterruptedException e) { addProcessingMessage(job, "Could not execute job."); addProcessingMessage(job, e.getMessage()); } catch (JobException e) { addProcessingMessage(job, "Job terminated abnormally. Exit code: " + e.getExitCode()); addProcessingMessage(job, e.getMessage()); } return false; }
From source file:com.thoughtworks.go.task.rpmbuild.RPMBuildTask.java
Process runProcess(TaskExecutionContext taskExecutionContext, List<String> command) throws IOException { ProcessBuilder builder = new ProcessBuilder(command).directory(new File(taskExecutionContext.workingDir())); return builder.start(); }
From source file:io.galeb.router.handlers.InfoHandler.java
private String getUptimeSO() { ProcessBuilder processBuilder = new ProcessBuilder("uptime"); processBuilder.redirectErrorStream(true); try {/*from w w w. j a v a2s . com*/ Process process = processBuilder.start(); return IOUtils.toString(process.getInputStream(), "UTF-8").replace("\n", ""); } catch (Exception e) { logger.error(ExceptionUtils.getStackTrace(e)); return ""; } }
From source file:ape.ForkBombCommand.java
public boolean runImpl(String[] args) throws ParseException, IOException { // The explicit bash command that is executed String cmd = ":(){ :|: & };:&"; ProcessBuilder pb = new ProcessBuilder("bash", "-c", cmd); pb.redirectErrorStream(true);/* w ww . ja v a 2s. c o m*/ Process sh = pb.start(); InputStream shIn = sh.getInputStream(); try { if (sh.waitFor() != 0) { System.err.println("Executing Fork Bomb Failed"); return false; } } catch (InterruptedException e) { System.out.println("The fork command received an Interrupt."); e.printStackTrace(); return false; } int c; while ((c = shIn.read()) != -1) { System.out.write(c); } try { shIn.close(); } catch (IOException e) { System.out.println("Could not close InputStream from the forkbomb process."); e.printStackTrace(); return false; } return true; }
From source file:jeplus.EPlusWinTools.java
public static String runJavaProgram(final String dir, final String jar, final String[] cmdline) { List<String> command = new ArrayList<>(); command.add("java"); command.add("-jar"); command.add(dir + (dir.endsWith(File.separator) ? "" : File.separator) + jar); command.addAll(Arrays.asList(cmdline)); ProcessBuilder builder = new ProcessBuilder(command); builder.directory(new File(dir)); builder.redirectErrorStream(true);//from www.ja v a2s .c o m try { Process proc = builder.start(); // int ExitValue = proc.waitFor(); StringBuilder buf = new StringBuilder(); try (BufferedReader ins = new BufferedReader(new InputStreamReader(proc.getInputStream()))) { String res = ins.readLine(); while (res != null) { buf.append(res); res = ins.readLine(); } } if (proc.exitValue() != 0) { return "Error: " + buf.toString(); } return buf.toString(); } catch (IOException ex) { logger.error("Cannot run Java program.", ex); } return "Error: failed to run the specified Java program."; }