Here you can find the source of runCommand(String[] cmdArray, String extraPath, String libPath)
public static Process runCommand(String[] cmdArray, String extraPath, String libPath) throws IOException
//package com.java2s; /**/*from w w w . j a v a 2 s. c o m*/ * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Map; public class Main { public static Process runCommand(String command, String extraPath, String libPath) throws IOException { return runCommand(new String[] { "/bin/bash", "-c", command }, extraPath, libPath); } public static Process runCommand(String[] cmdArray, String extraPath, String libPath) throws IOException { ProcessBuilder processBuilder = new ProcessBuilder(cmdArray); Map<String, String> environment = processBuilder.environment(); if (extraPath != null) { if (extraPath.isEmpty()) extraPath = "."; environment.put("PATH", extraPath + ":" + environment.get("PATH")); } if (libPath != null) { if (libPath.isEmpty()) libPath = "."; environment.put("LD_LIBRARY_PATH", libPath + ":" + environment.get("LD_LIBRARY_PATH")); } return processBuilder.start(); } public static String runCommand(String command, Integer expectedExitCode, String extraPath, String libPath) throws IOException { return runCommand(command, expectedExitCode, extraPath, libPath, true); } public static String runCommand(String command, Integer expectedExitCode, String extraPath, String libPath, boolean destory) throws IOException { BufferedReader stdOutputReader = null; StringBuilder output = new StringBuilder(); Process process = runCommand(command, extraPath, libPath); ; try { stdOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; while ((line = stdOutputReader.readLine()) != null) { output.append(line).append('\n'); } if (output.length() > 0) output.deleteCharAt(output.length() - 1); if (expectedExitCode != null && process.waitFor() != expectedExitCode) throw new IOException( "exit code=" + process.exitValue() + ", expected exit code=" + expectedExitCode); } catch (Throwable t) { destory = true; throw new IOException("fail to exceute " + command + ", output=" + output + (extraPath == null ? "" : ", extraPath=" + extraPath) + (libPath == null ? "" : ", libPath=" + libPath), t); } finally { if (stdOutputReader != null) stdOutputReader.close(); process.getOutputStream().close(); process.getInputStream().close(); process.getErrorStream().close(); if (destory) process.destroy(); } return output.toString(); } }