All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cedarsoftware.util.Executor Maven / Gradle / Ivy

The newest version!
package com.cedarsoftware.util;

import java.io.File;

/**
 * This class is used in conjunction with the Executor class.  Example
 * usage:
 * Executor exec = new Executor()
 * exec.execute("ls -l")
 * String result = exec.getOut()
 * 
* * @author John DeRegnaucourt ([email protected]) *
* Copyright (c) Cedar Software LLC *

* 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 *

* License *

* 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. */ public class Executor { private String _error; private String _out; public int exec(String command) { try { Process proc = Runtime.getRuntime().exec(command); return runIt(proc); } catch (Exception e) { System.err.println("Error occurred executing command: " + command); e.printStackTrace(System.err); return -1; } } public int exec(String[] cmdarray) { try { Process proc = Runtime.getRuntime().exec(cmdarray); return runIt(proc); } catch (Exception e) { System.err.println("Error occurred executing command: " + cmdArrayToString(cmdarray)); e.printStackTrace(System.err); return -1; } } public int exec(String command, String[] envp) { try { Process proc = Runtime.getRuntime().exec(command, envp); return runIt(proc); } catch (Exception e) { System.err.println("Error occurred executing command: " + command); e.printStackTrace(System.err); return -1; } } public int exec(String[] cmdarray, String[] envp) { try { Process proc = Runtime.getRuntime().exec(cmdarray, envp); return runIt(proc); } catch (Exception e) { System.err.println("Error occurred executing command: " + cmdArrayToString(cmdarray)); e.printStackTrace(System.err); return -1; } } public int exec(String command, String[] envp, File dir) { try { Process proc = Runtime.getRuntime().exec(command, envp, dir); return runIt(proc); } catch (Exception e) { System.err.println("Error occurred executing command: " + command); e.printStackTrace(System.err); return -1; } } public int exec(String[] cmdarray, String[] envp, File dir) { try { Process proc = Runtime.getRuntime().exec(cmdarray, envp, dir); return runIt(proc); } catch (Exception e) { System.err.println("Error occurred executing command: " + cmdArrayToString(cmdarray)); e.printStackTrace(System.err); return -1; } } private int runIt(Process proc) throws InterruptedException { StreamGobbler errors = new StreamGobbler(proc.getErrorStream()); Thread errorGobbler = new Thread(errors); StreamGobbler out = new StreamGobbler(proc.getInputStream()); Thread outputGobbler = new Thread(out); errorGobbler.start(); outputGobbler.start(); int exitVal = proc.waitFor(); errorGobbler.join(); outputGobbler.join(); _error = errors.getResult(); _out = out.getResult(); return exitVal; } /** * @return String content written to StdErr */ public String getError() { return _error; } /** * @return String content written to StdOut */ public String getOut() { return _out; } private String cmdArrayToString(String[] cmdArray) { StringBuilder s = new StringBuilder(); for (String cmd : cmdArray) { s.append(cmd); s.append(' '); } return s.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy