Process output from apache-commons exec

后端 未结 4 1586
予麋鹿
予麋鹿 2020-12-10 10:11

I am at my wits end here. I\'m sure this is something simple and I most likely have huge holes in my understanding of java and streams. I think there are so many classes tha

4条回答
  •  误落风尘
    2020-12-10 11:13

    Based on James A Wilson's answer I created the helper class "Execute". It wraps his answer into a solution that also supplies the exitValue for convenience.

    A single line is necessary to execute a command this way:

    ExecResult result=Execute.execCmd(cmd,expectedExitCode);

    The following Junit Testcase tests and shows how to use it:

    Junit4 test case:

    package com.bitplan.newsletter;
    
    import static org.junit.Assert.*;
    
    import java.util.List;
    
    import org.junit.Test;
    
    import com.bitplan.cmd.Execute;
    import com.bitplan.cmd.Execute.ExecResult;
    
    /**
     * test case for the execute class
     * @author wf
     *
     */
    public class TestExecute {
         @Test
       public void testExecute() throws Exception {
         String cmd="/bin/ls";
         ExecResult result = Execute.execCmd(cmd,0);
         assertEquals(0,result.getExitCode());
         List lines = result.getLines();
         assertTrue(lines.size()>0);
         for (String line:lines) {
             System.out.println(line);
         }
       }
    }
    

    Execute Java helper Class:

    package com.bitplan.cmd;
    
    import java.util.LinkedList;
    import java.util.List;
    import java.util.logging.Level;
    
    import org.apache.commons.exec.CommandLine;
    import org.apache.commons.exec.DefaultExecutor;
    import org.apache.commons.exec.LogOutputStream;
    import org.apache.commons.exec.PumpStreamHandler;
    
    /**
     * Execute helper using apache commons exed
     *
     *  add this dependency to your pom.xml:
       
                org.apache.commons
                commons-exec
                1.2
            
    
     * @author wf
     *
     */
    public class Execute {
    
        protected static java.util.logging.Logger LOGGER = java.util.logging.Logger
                .getLogger("com.bitplan.cmd");
    
        protected final static boolean debug=true;
    
        /**
         * LogOutputStream
         * http://stackoverflow.com/questions/7340452/process-output-from
         * -apache-commons-exec
         * 
         * @author wf
         * 
         */
        public static class ExecResult extends LogOutputStream {
            private int exitCode;
            /**
             * @return the exitCode
             */
            public int getExitCode() {
                return exitCode;
            }
    
            /**
             * @param exitCode the exitCode to set
             */
            public void setExitCode(int exitCode) {
                this.exitCode = exitCode;
            }
    
            private final List lines = new LinkedList();
    
            @Override
            protected void processLine(String line, int level) {
                lines.add(line);
            }
    
            public List getLines() {
                return lines;
            }
        }
    
        /**
         * execute the given command
         * @param cmd - the command 
         * @param exitValue - the expected exit Value
         * @return the output as lines and exit Code
         * @throws Exception
         */
        public static ExecResult execCmd(String cmd, int exitValue) throws Exception {
            if (debug)
                LOGGER.log(Level.INFO,"running "+cmd);
            CommandLine commandLine = CommandLine.parse(cmd);
            DefaultExecutor executor = new DefaultExecutor();
            executor.setExitValue(exitValue);
            ExecResult result =new ExecResult();
            executor.setStreamHandler(new PumpStreamHandler(result));
            result.setExitCode(executor.execute(commandLine));
            return result;
        }
    
    }
    

提交回复
热议问题