Here you can find the source of execCommand(String command)
Parameter | Description |
---|---|
command | a parameter |
public static String execCommand(String command)
//package com.java2s; /******************************************************************************* * Copyright 2012 Internet2//from w w w .j av a 2 s . co m * * Licensed 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.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; public class Main { /** * The name says it all. */ private static final int DEFAULT_BUFFER_SIZE = 1024 * 4; /** * execute a command and return the output * @param command * @return the output (end errors), and trim the output */ public static String execCommand(String command) { Runtime runtime = Runtime.getRuntime(); Process process = null; BufferedReader reader = null; StringBuffer result = new StringBuffer(); try { process = runtime.exec(command); reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = null; while ((line = reader.readLine()) != null) { result.append(line); } } catch (IOException ioe) { throw new RuntimeException(ioe); } finally { try { if (reader != null) { reader.close(); } } catch (Exception e) { e.printStackTrace(); } } String resultString = result.toString(); return resultString.trim(); } /** * Get the contents of an <code>InputStream</code> as a String. * @param input the <code>InputStream</code> to read from * @param encoding The name of a supported character encoding. See the * <a href="http://www.iana.org/assignments/character-sets">IANA * Charset Registry</a> for a list of valid encoding types. * @return the requested <code>String</code> * @throws IOException In case of an I/O problem */ public static String toString(InputStream input, String encoding) throws IOException { StringWriter sw = new StringWriter(); copy(input, sw, encoding); return sw.toString(); } /** * <p>Removes control characters (char <= 32) from both * ends of this String, handling <code>null</code> by returning * <code>null</code>.</p> * * <p>The String is trimmed using {@link String#trim()}. * Trim removes start and end characters <= 32. * To strip whitespace use strip(String)</p> * * <p>To trim your choice of characters, use the * strip(String, String) methods.</p> * * <pre> * StringUtils.trim(null) = null * StringUtils.trim("") = "" * StringUtils.trim(" ") = "" * StringUtils.trim("abc") = "abc" * StringUtils.trim(" abc ") = "abc" * </pre> * * @param str the String to be trimmed, may be null * @return the trimmed string, <code>null</code> if null String input */ public static String trim(String str) { return str == null ? null : str.trim(); } /** * Copy and convert bytes from an <code>InputStream</code> to chars on a * <code>Writer</code>, using the specified encoding. * @param input the <code>InputStream</code> to read from * @param output the <code>Writer</code> to write to * @param encoding The name of a supported character encoding. See the * <a href="http://www.iana.org/assignments/character-sets">IANA * Charset Registry</a> for a list of valid encoding types. * @throws IOException In case of an I/O problem */ public static void copy(InputStream input, Writer output, String encoding) throws IOException { InputStreamReader in = new InputStreamReader(input, encoding); copy(in, output); } /** * Copy chars from a <code>Reader</code> to a <code>Writer</code>. * @param input the <code>Reader</code> to read from * @param output the <code>Writer</code> to write to * @return the number of characters copied * @throws IOException In case of an I/O problem */ public static int copy(Reader input, Writer output) throws IOException { char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int count = 0; int n = 0; while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); count += n; } return count; } }