List of usage examples for java.lang ProcessBuilder environment
Map environment
To view the source code for java.lang ProcessBuilder environment.
Click Source Link
From source file:org.broadinstitute.gatk.utils.runtime.ProcessController.java
/** * Executes a command line program with the settings and waits for it to return, * processing the output on a background thread. * * @param settings Settings to be run.//from w w w.jav a 2s. c o m * @return The output of the command. */ public ProcessOutput exec(ProcessSettings settings) { if (destroyed) throw new IllegalStateException("This controller was destroyed"); ProcessBuilder builder = new ProcessBuilder(settings.getCommand()); builder.directory(settings.getDirectory()); Map<String, String> settingsEnvironment = settings.getEnvironment(); if (settingsEnvironment != null) { Map<String, String> builderEnvironment = builder.environment(); builderEnvironment.clear(); builderEnvironment.putAll(settingsEnvironment); } builder.redirectErrorStream(settings.isRedirectErrorStream()); StreamOutput stdout = null; StreamOutput stderr = null; // Start the process running. try { synchronized (toCapture) { process = builder.start(); } running.add(this); } catch (IOException e) { String message = String.format("Unable to start command: %s\nReason: %s", StringUtils.join(builder.command(), " "), e.getMessage()); throw new ReviewedGATKException(message); } int exitCode; try { // Notify the background threads to start capturing. synchronized (toCapture) { toCapture.put(ProcessStream.Stdout, new CapturedStreamOutput(settings.getStdoutSettings(), process.getInputStream(), System.out)); toCapture.put(ProcessStream.Stderr, new CapturedStreamOutput(settings.getStderrSettings(), process.getErrorStream(), System.err)); toCapture.notifyAll(); } // Write stdin content InputStreamSettings stdinSettings = settings.getStdinSettings(); Set<StreamLocation> streamLocations = stdinSettings.getStreamLocations(); if (!streamLocations.isEmpty()) { try { OutputStream stdinStream = process.getOutputStream(); for (StreamLocation location : streamLocations) { InputStream inputStream; switch (location) { case Buffer: inputStream = new ByteArrayInputStream(stdinSettings.getInputBuffer()); break; case File: try { inputStream = FileUtils.openInputStream(stdinSettings.getInputFile()); } catch (IOException e) { throw new UserException.BadInput(e.getMessage()); } break; case Standard: inputStream = System.in; break; default: throw new ReviewedGATKException("Unexpected stream location: " + location); } try { IOUtils.copy(inputStream, stdinStream); } finally { if (location != StreamLocation.Standard) IOUtils.closeQuietly(inputStream); } } stdinStream.flush(); } catch (IOException e) { throw new ReviewedGATKException( "Error writing to stdin on command: " + StringUtils.join(builder.command(), " "), e); } } // Wait for the process to complete. try { process.getOutputStream().close(); process.waitFor(); } catch (IOException e) { throw new ReviewedGATKException( "Unable to close stdin on command: " + StringUtils.join(builder.command(), " "), e); } catch (InterruptedException e) { throw new ReviewedGATKException("Process interrupted", e); } finally { while (!destroyed && stdout == null || stderr == null) { synchronized (fromCapture) { if (fromCapture.containsKey(ProcessStream.Stdout)) stdout = fromCapture.remove(ProcessStream.Stdout); if (fromCapture.containsKey(ProcessStream.Stderr)) stderr = fromCapture.remove(ProcessStream.Stderr); try { if (stdout == null || stderr == null) fromCapture.wait(); } catch (InterruptedException e) { // Log the error, ignore the interrupt and wait patiently // for the OutputCaptures to (via finally) return their // stdout and stderr. logger.error(e); } } } if (destroyed) { if (stdout == null) stdout = StreamOutput.EMPTY; if (stderr == null) stderr = StreamOutput.EMPTY; } } } finally { synchronized (toCapture) { exitCode = process.exitValue(); process = null; } running.remove(this); } return new ProcessOutput(exitCode, stdout, stderr); }
From source file:org.apache.hadoop.streaming.PipeMapRed.java
public void configure(JobConf job) { try {//from w w w.j av a2 s .c om String argv = getPipeCommand(job); joinDelay_ = job.getLong("stream.joindelay.milli", 0); job_ = job; fs_ = FileSystem.get(job_); mapInputWriterClass_ = job_.getClass("stream.map.input.writer.class", TextInputWriter.class, InputWriter.class); mapOutputReaderClass_ = job_.getClass("stream.map.output.reader.class", TextOutputReader.class, OutputReader.class); reduceInputWriterClass_ = job_.getClass("stream.reduce.input.writer.class", TextInputWriter.class, InputWriter.class); reduceOutputReaderClass_ = job_.getClass("stream.reduce.output.reader.class", TextOutputReader.class, OutputReader.class); nonZeroExitIsFailure_ = job_.getBoolean("stream.non.zero.exit.is.failure", true); doPipe_ = getDoPipe(); if (!doPipe_) return; setStreamJobDetails(job); String[] argvSplit = splitArgs(argv); String prog = argvSplit[0]; File currentDir = new File(".").getAbsoluteFile(); if (new File(prog).isAbsolute()) { // we don't own it. Hope it is executable } else { FileUtil.chmod(new File(currentDir, prog).toString(), "a+x"); } // // argvSplit[0]: // An absolute path should be a preexisting valid path on all TaskTrackers // A relative path is converted into an absolute pathname by looking // up the PATH env variable. If it still fails, look it up in the // tasktracker's local working directory // if (!new File(argvSplit[0]).isAbsolute()) { PathFinder finder = new PathFinder("PATH"); finder.prependPathComponent(currentDir.toString()); File f = finder.getAbsolutePath(argvSplit[0]); if (f != null) { argvSplit[0] = f.getAbsolutePath(); } f = null; } logprintln("PipeMapRed exec " + Arrays.asList(argvSplit)); Environment childEnv = (Environment) StreamUtil.env().clone(); addJobConfToEnvironment(job_, childEnv); addEnvironment(childEnv, job_.get("stream.addenvironment")); // add TMPDIR environment variable with the value of java.io.tmpdir envPut(childEnv, "TMPDIR", System.getProperty("java.io.tmpdir")); // Start the process ProcessBuilder builder = new ProcessBuilder(argvSplit); builder.environment().putAll(childEnv.toMap()); sim = builder.start(); clientOut_ = new DataOutputStream(new BufferedOutputStream(sim.getOutputStream(), BUFFER_SIZE)); clientIn_ = new DataInputStream(new BufferedInputStream(sim.getInputStream(), BUFFER_SIZE)); clientErr_ = new DataInputStream(new BufferedInputStream(sim.getErrorStream())); startTime_ = System.currentTimeMillis(); errThread_ = new MRErrorThread(); errThread_.start(); } catch (Exception e) { logStackTrace(e); LOG.error("configuration exception", e); throw new RuntimeException("configuration exception", e); } }
From source file:org.ballerinalang.test.context.ServerInstance.java
/** * Executing the sh or bat file to start the server. * * @param args - command line arguments to pass when executing the sh or bat file * @param envProperties - environmental properties to be appended to the environment * * @throws BallerinaTestException if starting services failed *///from w w w .j a v a 2 s . c o m private void startServer(String[] args, Map<String, String> envProperties) throws BallerinaTestException { String scriptName = Constant.BALLERINA_SERVER_SCRIPT_NAME; String[] cmdArray; File commandDir = new File(serverHome); try { if (Utils.getOSName().toLowerCase(Locale.ENGLISH).contains("windows")) { commandDir = new File(serverHome + File.separator + "bin"); cmdArray = new String[] { "cmd.exe", "/c", scriptName + ".bat", "run" }; } else { cmdArray = new String[] { "bash", "bin/" + scriptName, "run" }; } String[] cmdArgs = Stream.concat(Arrays.stream(cmdArray), Arrays.stream(args)).toArray(String[]::new); ProcessBuilder processBuilder = new ProcessBuilder(cmdArgs).directory(commandDir); if (envProperties != null) { Map<String, String> env = processBuilder.environment(); for (Map.Entry<String, String> entry : envProperties.entrySet()) { env.put(entry.getKey(), entry.getValue()); } } process = processBuilder.start(); } catch (IOException e) { throw new BallerinaTestException("Error starting services", e); } }
From source file:com.netflix.genie.server.jobmanager.impl.JobManagerImpl.java
/** * Set the command and application for a given process and job. * * @param processBuilder The process builder to use. * @throws GenieException On an error interacting with database. *///w w w . j a v a 2 s.c o m private void setCommandAndApplicationForJob(final ProcessBuilder processBuilder) throws GenieException { final Command command = this.commandService.getCommand(this.job.getCommandId()); if (command.getConfigs() != null && !command.getConfigs().isEmpty()) { processBuilder.environment().put("S3_COMMAND_CONF_FILES", convertCollectionToString(command.getConfigs())); } if (StringUtils.isNotBlank(command.getEnvPropFile())) { processBuilder.environment().put("COMMAND_ENV_FILE", command.getEnvPropFile()); } final Application application = command.getApplication(); if (application != null) { if (application.getConfigs() != null && !application.getConfigs().isEmpty()) { processBuilder.environment().put("S3_APPLICATION_CONF_FILES", convertCollectionToString(application.getConfigs())); } if (application.getJars() != null && !application.getJars().isEmpty()) { processBuilder.environment().put("S3_APPLICATION_JAR_FILES", convertCollectionToString(application.getJars())); } if (StringUtils.isNotBlank(application.getEnvPropFile())) { processBuilder.environment().put("APPLICATION_ENV_FILE", application.getEnvPropFile()); } } }
From source file:au.com.permeance.liferay.portlet.patchingtoolinfo.cli.PatchingToolCommandRunner.java
public void runCommand() throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("running patching tool command ..."); }/* ww w. j a v a2 s .c o m*/ try { ProcessBuilder processBuilder = configureProcessBuilder(); // NOTE: ProcessBuilder#environent is initialised with System.getenv() // @see http://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html#environment%28%29 if (LOG.isDebugEnabled()) { LOG.debug("processBuilder : " + processBuilder); List<String> commandList = processBuilder.command(); LOG.debug("command environment : " + processBuilder.environment()); LOG.debug("command list : " + commandList); LOG.debug("command directory : " + processBuilder.directory()); } if (LOG.isDebugEnabled()) { List<String> commandList = processBuilder.command(); String processCommandStr = StringHelper.flattenStringList(commandList); LOG.debug("running patching tool command : " + processCommandStr); } Process process = processBuilder.start(); if (LOG.isDebugEnabled()) { LOG.debug("process : " + process); } // NOTE: Java 1.8 supports Process#waitFor with a timeout // eg. boolean finished = iostat.waitFor(100, TimeUnit.MILLISECONDS); int processExitValue = process.waitFor(); List<String> processOutputLines = IOUtils.readLines(process.getInputStream()); List<String> processErrorLines = IOUtils.readLines(process.getErrorStream()); this.patchingToolResults = new PatchingToolResults(processExitValue, processOutputLines, processErrorLines); if (LOG.isDebugEnabled()) { LOG.debug("patchingToolResults: " + patchingToolResults); } if (LOG.isDebugEnabled()) { LOG.debug("patching tool returned exit code " + this.patchingToolResults.getExitValue()); LOG.debug("patching tool returned " + this.patchingToolResults.getOutputLines().size() + " output lines"); LOG.debug("--- COMMAND OUTPUT ---"); LOG.debug(processOutputLines); LOG.debug("patching tool returned " + this.patchingToolResults.getErrorLines().size() + " error lines"); LOG.debug("--- COMMAND ERROR ---"); LOG.debug(processErrorLines); } // NOTE: Command shell may return lines in the error stream that are warning messages, not errors. // Hence, we cannot rely upon content in the error stream as a valid error. if (this.patchingToolResults.getExitValue() != 0) { StringBuilder sb = new StringBuilder(); String errorLine1 = null; if (this.patchingToolResults.hasErrorLines()) { errorLine1 = this.patchingToolResults.getErrorLines().get(0); } if (errorLine1 == null) { sb.append("Error running patching tool command."); sb.append(" See portal logs for more details."); } else { sb.append("Error running patching tool command : "); sb.append(errorLine1); } String errMsg = sb.toString(); throw new Exception(errMsg); } } catch (Exception e) { String msg = "Error executing patching tool command : " + e.getMessage(); LOG.error(msg, e); throw new Exception(msg, e); } }
From source file:com.gochinatv.datasync.util.Shell.java
/** * Run a command/*from w w w . j a va 2 s . c om*/ */ private void runCommand() throws IOException { ProcessBuilder builder = new ProcessBuilder(getExecString()); Timer timeOutTimer = null; ShellTimeoutTimerTask timeoutTimerTask; timedOut = new AtomicBoolean(false); completed = new AtomicBoolean(false); if (environment != null) { builder.environment().putAll(this.environment); } if (dir != null) { builder.directory(this.dir); } process = builder.start(); if (timeOutInterval > 0) { timeOutTimer = new Timer(); timeoutTimerTask = new ShellTimeoutTimerTask(this); //One time scheduling. timeOutTimer.schedule(timeoutTimerTask, timeOutInterval); } final BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); final StringBuffer errMsg = new StringBuffer(); // read error and input streams as this would free up the buffers // free the error stream buffer Thread errThread = new Thread() { @Override public void run() { try { String line = errReader.readLine(); while ((line != null) && !isInterrupted()) { errMsg.append(line); errMsg.append(System.getProperty("line.separator")); line = errReader.readLine(); } } catch (IOException ioe) { LOG.warn("Error reading the error stream", ioe); } } }; try { errThread.start(); } catch (IllegalStateException ignored) { } try { parseExecResult(inReader); // parse the output // clear the input stream buffer String line = inReader.readLine(); while (line != null) { line = inReader.readLine(); } // wait for the process to finish and check the exit code exitCode = process.waitFor(); try { // make sure that the error thread exits errThread.join(); } catch (InterruptedException ie) { LOG.warn("Interrupted while reading the error stream", ie); } completed.set(true); //the timeout thread handling //taken care in finally block if (exitCode != 0) { throw new ExitCodeException(exitCode, errMsg.toString()); } } catch (InterruptedException ie) { throw new IOException(ie.toString()); } finally { if ((timeOutTimer != null) && !timedOut.get()) { timeOutTimer.cancel(); } // close the input stream try { inReader.close(); } catch (IOException ioe) { LOG.warn("Error while closing the input stream", ioe); } if (!completed.get()) { errThread.interrupt(); } try { errReader.close(); } catch (IOException ioe) { LOG.warn("Error while closing the error stream", ioe); } process.destroy(); lastTime = System.currentTimeMillis(); } }
From source file:dk.netarkivet.harvester.harvesting.controller.AbstractJMXHeritrixController.java
/** * Write various info on the system we're using into the given file. This * info will later get put into metadata for the crawl. * * @param outputFile/* w w w. ja v a 2 s . c om*/ * A file to write to. * @param builder * The ProcessBuilder being used to start the Heritrix process */ @SuppressWarnings("unchecked") private void writeSystemInfo(File outputFile, ProcessBuilder builder) { PrintWriter writer = null; try { writer = new PrintWriter(new FileWriter(outputFile)); writer.println("The Heritrix process is started in the following" + " environment\n (note that some entries will be" + " changed by the starting JVM):"); Map<String, String> env = builder.environment(); List<String> keyList = new ArrayList<String>(env.keySet()); Collections.sort(keyList); for (String key : keyList) { writer.println(key + "=" + env.get(key)); } writer.println("Process properties:"); Properties properties = System.getProperties(); keyList = new ArrayList<String>((Set) properties.keySet()); Collections.sort(keyList); for (String key : keyList) { writer.println(key + "=" + properties.get(key)); } } catch (IOException e) { log.warn("Error writing basic properties to output file.", e); } finally { if (writer != null) { writer.close(); } } }
From source file:io.smartspaces.util.process.BaseNativeApplicationRunner.java
/** * Attempt the run./*from ww w .j av a 2 s . com*/ * * @param firstTime * {@code true} if this is the first attempt * * @return the process that was created * * @throws SmartSpacesException * was not able to start the process the first time */ private Process attemptRun(boolean firstTime) throws SmartSpacesException { try { ProcessBuilder builder = new ProcessBuilder(commandLine); Map<String, String> processEnvironment = builder.environment(); if (cleanEnvironment) { processEnvironment.clear(); } modifyEnvironment(processEnvironment, environment); // If don't know the executable folder, the executable will be run in the // process directory of the Java program. if (executableFolder != null) { builder.directory(executableFolder); log.info(String.format("Starting up native code in folder %s", executableFolder.getAbsolutePath())); } return builder.start(); } catch (Exception e) { // Placed here so we can get the exception when thrown. if (firstTime) { runnerState.set(NativeApplicationRunnerState.STARTUP_FAILED); handleApplicationStartupFailed(); throw SmartSpacesException.newFormattedException(e, "Can't start up native application " + executablePath); } return null; } }
From source file:org.apache.hadoop.mapred.util.Shell.java
/** Run a command */ private void runCommand() throws IOException { ProcessBuilder builder = new ProcessBuilder(getExecString()); Timer timeOutTimer = null;// ww w. j a v a 2s .co m ShellTimeoutTimerTask timeoutTimerTask = null; timedOut = new AtomicBoolean(false); completed = new AtomicBoolean(false); if (environment != null) { builder.environment().putAll(this.environment); } if (dir != null) { builder.directory(this.dir); } process = builder.start(); if (timeOutInterval > 0) { timeOutTimer = new Timer(); timeoutTimerTask = new ShellTimeoutTimerTask(this); //One time scheduling. timeOutTimer.schedule(timeoutTimerTask, timeOutInterval); } final BufferedReader errReader = new BufferedReader(new InputStreamReader(process.getErrorStream())); BufferedReader inReader = new BufferedReader(new InputStreamReader(process.getInputStream())); final StringBuffer errMsg = new StringBuffer(); // read error and input streams as this would free up the buffers // free the error stream buffer Thread errThread = new Thread() { @Override public void run() { try { String line = errReader.readLine(); while ((line != null) && !isInterrupted()) { errMsg.append(line); errMsg.append(System.getProperty("line.separator")); line = errReader.readLine(); } } catch (IOException ioe) { LOG.warn("Error reading the error stream", ioe); } } }; try { errThread.start(); } catch (IllegalStateException ise) { } try { parseExecResult(inReader); // parse the output // clear the input stream buffer String line = inReader.readLine(); while (line != null) { line = inReader.readLine(); } // wait for the process to finish and check the exit code exitCode = process.waitFor(); try { // make sure that the error thread exits errThread.join(); } catch (InterruptedException ie) { LOG.warn("Interrupted while reading the error stream", ie); } completed.set(true); //the timeout thread handling //taken care in finally block if (exitCode != 0) { throw new ExitCodeException(exitCode, errMsg.toString()); } } catch (InterruptedException ie) { throw new IOException(ie.toString()); } finally { if ((timeOutTimer != null) && !timedOut.get()) { timeOutTimer.cancel(); } // close the input stream try { inReader.close(); } catch (IOException ioe) { LOG.warn("Error while closing the input stream", ioe); } if (!completed.get()) { errThread.interrupt(); } try { errReader.close(); } catch (IOException ioe) { LOG.warn("Error while closing the error stream", ioe); } process.destroy(); lastTime = System.currentTimeMillis(); } }
From source file:de.hopmann.msc.slave.util.RCMDBuilder.java
public RCMDOutputReader start() throws IOException { List<String> commands = new ArrayList<String>(); commands.add(rExecutablePath.toAbsolutePath().toString()); commands.add("CMD"); processCommands(commands);/* ww w. j a va 2s . co m*/ if (targetLibraryPath != null) { commands.add("--library=" + targetLibraryPath.toAbsolutePath().toString()); } commands.add(packagePath.toAbsolutePath().toString()); ProcessBuilder cmdProcessBuilder = new ProcessBuilder(commands) .directory(workingDirectoryPath.toAbsolutePath().toFile()).redirectErrorStream(true); if (!sourceLibraryPaths.isEmpty()) { StringBuilder rlibs = new StringBuilder(); for (Path libPath : sourceLibraryPaths) { if (rlibs.length() != 0) { // Add separator char rlibs.append(SystemUtils.IS_OS_WINDOWS ? ";" : ":"); } rlibs.append(libPath.toAbsolutePath().toString()); } Map<String, String> processEnvironment = cmdProcessBuilder.environment(); processEnvironment.put(R_LIBS_ENVIR, rlibs.toString()); processEnvironment.put(R_LANGUAGE_ENVIR, "en"); processEnvironment.put(LC_ALL, "English"); processEnvironment.put(CYQWIN_ENVIR, "nodosfilewarning"); for (Entry<String, String> envirEntry : customEnvironment.entrySet()) { processEnvironment.put(envirEntry.getKey(), envirEntry.getValue()); } } processEnvironment(cmdProcessBuilder.environment()); final Process cmdProcess = cmdProcessBuilder.start(); // BufferedReader processOut = new BufferedReader( // new InputStreamReader(installProcess.getInputStream())); // while (processOut.readLine() != null) { } // IOUtils.copy(installProcess.getInputStream(), System.out); return new RCMDOutputReader(cmdProcess, logPrintStream) { @Override public void close() throws IOException { try { cmdProcess.waitFor();// XXX or .destroy()? } catch (InterruptedException e) { } super.close(); } }; }