execrunner.java
来自「opennms得相关源码 请大家看看」· Java 代码 · 共 465 行 · 第 1/2 页
JAVA
465 行
//// This file is part of the OpenNMS(R) Application.//// OpenNMS(R) is Copyright (C) 2002-2003 The OpenNMS Group, Inc. All rights reserved.// OpenNMS(R) is a derivative work, containing both original code, included code and modified// code that was published under the GNU General Public License. Copyrights for modified // and included code are below.//// OpenNMS(R) is a registered trademark of The OpenNMS Group, Inc.//// For more information contact: // OpenNMS Licensing <license@opennms.org>// http://www.opennms.org/// http://www.opennms.com///package org.opennms.netmgt.utils;////////////////////////////////////////////////////////////////////////////////// Copyright (C) 2002 Scott McCrory//// This program is free software; you can redistribute it and/or// modify it under the terms of the GNU General Public License// as published by the Free Software Foundation; either version 2// of the License, or (at your option) any later version.//// This program is distributed in the hope that it will be useful,// but WITHOUT ANY WARRANTY; without even the implied warranty of// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the// GNU General Public License for more details.//// You should have received a copy of the GNU General Public License// along with this program; if not, write to the Free Software// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.////////////////////////////////////////////////////////////////////////////////import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.io.PrintWriter;import java.io.StringWriter;import java.util.Date;import java.util.StringTokenizer;/** * <P> * Runs external executables, optionally under a watched thread. * * In addition, probably the most useful feature of ExecRunner is using it to * run a command-line program and obtain its stdout and stderr results in two * strings. This is done with exec(String) - see that method for an example. * * With acknowledgements to Michael C. Daconta, author of "Java Pitfalls, Time * Saving Solutions, and Workarounds to Improve Programs." and his article in * JavaWorld "When Runtime.exec() Won't". * </P> * * @author <a href="mailto:smccrory@users.sourceforge.net">Scott McCrory </a>. * @version CVS $Id: ExecRunner.java 1199 2004-11-21 03:31:16Z brozow $ */public class ExecRunner { /** Win NT/2K/MEPro require cmd.exe to run programs * */ private static final String WINDOWS_NT_2000_COMMAND_1 = "cmd.exe"; /** Win NT/2K/MEPro require the /C to specify what to run * */ private static final String WINDOWS_NT_2000_COMMAND_2 = "/C"; /** Win 9X/MEHome require cmd.exe to run programs * */ private static final String WINDOWS_9X_ME_COMMAND_1 = "command.exe"; /** Win 9X/MEHome require the /C to specify what to run * */ private static final String WINDOWS_9X_ME_COMMAND_2 = "/C"; /** String to send to STDERR if program exceeds max run time * */ private static final String MAX_RUN_TIME_EXCEEDED_STRING = "MAX_RUN_TIME_EXCEEDED"; /** String to capture STDOUT * */ private String out = new String(); /** String to capture STDERR * */ private String err = new String(); /** Default max run time (in seconds) * */ private int maxRunTimeSecs = 0; /** Flag to indicate if we've exceeded max run time * */ private boolean maxRunTimeExceeded = false; /** The name of this class for logging * */ private static final String CLASS_NAME = "ExecRunner"; /** The version of this class (filled in by CVS) * */ private static final String VERSION = "CVS $Revision: 1199 $"; /** Number of miliseconds to wait between polling watched thread * */ private static final int POLL_DELAY_MS = 100; /** * Basic ExecRunner constructor. * */ public ExecRunner() { super(); } /** * ExecRunner constructor which also conveniently runs exec(String). * * @param command * The program or command to run * @throws ExceptionInInitializerError * thrown if a problem occurs */ public ExecRunner(String command) throws ExceptionInInitializerError { this(); try { exec(command); } catch (IOException ioe) { throw new ExceptionInInitializerError(ioe.getMessage()); } catch (InterruptedException inte) { throw new ExceptionInInitializerError(inte.getMessage()); } } /** * We override the <code>clone</code> method here to prevent cloning of * our class. * * @throws CloneNotSupportedException * To indicate cloning is not allowed * @return Nothing ever really returned since we throw a * CloneNotSupportedException */ public final Object clone() throws CloneNotSupportedException { throw new java.lang.CloneNotSupportedException(); } /** * The <B>exec(String) </B> method runs a process inside of a watched * thread. It returns the client's exit code and feeds its STDOUT and STDERR * to ExecRunner's out and err strings, where you then use getOutString() * and getErrString() to obtain these values. Example: * * <pre> * * // Execute the program and grab the results * try { * ExecRunner er = new ExecRunner(); * er.setMaxRunTimeSecs(5); * er.exec("ls -l"); * if (!er.getMaxRunTimeExceeded()) { * out = er.getOutString(); * err = er.getErrString(); * } else { * System.out.println("Maximum run time exceeded!"); * } * } catch (Exception e) { * System.out.println("Error executing " + program + ": " + e.getMessage()); * continue; * } * </pre> * * @return The command's return code * @param command * The program or command to run * @throws IOException * thrown if a problem occurs * @throws InterruptedException * thrown if a problem occurs */ public int exec(String command) throws IOException, InterruptedException { StringWriter swOut = new StringWriter(); PrintWriter pwOut = new PrintWriter(swOut, true); StringWriter swErr = new StringWriter(); PrintWriter pwErr = new PrintWriter(swErr, true); int rc = exec(command, pwOut, pwErr); out = swOut.toString(); err = swErr.toString(); return rc; } /** * Convenience method for calling exec with OutputStreams. * * @return The command's return code * @param command * The program or command to run * @param stdoutStream * java.io.OutputStream * @param stderrStream * java.io.OutputStream * @throws IOException * thrown if a problem occurs * @throws InterruptedException * thrown if a problem occurs */ public int exec(String command, OutputStream stdoutStream, OutputStream stderrStream) throws IOException, InterruptedException { PrintWriter pwOut = new PrintWriter(stdoutStream, true); PrintWriter pwErr = new PrintWriter(stderrStream, true); return exec(command, pwOut, pwErr); } /** * The <code>exec(String, PrintWriter, PrintWriter)</code> method runs a * process inside of a watched thread. It returns the client's exit code and * feeds its STDOUT and STDERR to the passed-in streams. * * @return The command's return code * @param command * The program or command to run * @param stdoutWriter * java.io.PrintWriter * @param stderrWriter * java.io.PrintWriter * @throws IOException * thrown if a problem occurs * @throws InterruptedException * thrown if a problem occurs */ public int exec(String command, PrintWriter stdoutWriter, PrintWriter stderrWriter) throws IOException, InterruptedException {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?