common-exec调用进程命令

来源:互联网 发布:java修改文件编码格式 编辑:程序博客网 时间:2024/05/16 12:44
package org.sf.exec;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.ExecuteWatchdog;
import org.apache.commons.exec.Executor;
import org.junit.Before;
import org.junit.Test;

/**
 * 返回结果为1表示正常,0不非常
 *
 * @author sfit0734
 *
 */
public class ExecTest {
    File file = null;

    @Before
    public void init() {
        file = new File("E:\\shunfeng\\java_doc\\pdf_test_file\\1.pdf");
    }

    /**
     * 会抛异常,原因,进程可能还没有退出
     *
     * @throws ExecuteException
     * @throws IOException
     */
    @Test
    public void test1() throws ExecuteException, IOException {
        String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
        line = "ping -t www.baidu.com";
        CommandLine cmdLine = CommandLine.parse(line);
        DefaultExecutor executor = new DefaultExecutor();
        int exitValue = executor.execute(cmdLine);

    }

    /**
     * 会抛异常,原因,进程可能还没有退出
     *
     * @throws ExecuteException
     * @throws IOException
     */
    @Test
    public void test2() throws ExecuteException, IOException {
        String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
        line = "ping  www.baidu.com";
        CommandLine cmdLine = CommandLine.parse(line);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(1);
        int exitValue = executor.execute(cmdLine);
        System.out.println("exitvalue:" + exitValue);

    }

    /**
     * * 会抛异常,原因,进程可能还没有退出
     *
     * @throws ExecuteException
     * @throws IOException
     *             一直在执行ping命令,但6秒后结束,是强行退出,但程序不会发生异常
     */
    @Test
    public void test3() throws ExecuteException, IOException {
        String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
        line = "ping -t  www.baidu.com";
        CommandLine cmdLine = CommandLine.parse(line);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(1);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(6 * 1000);
        executor.setWatchdog(watchdog);
        int exitValue = executor.execute(cmdLine);
        System.out.println(exitValue);

    }

    /**
     * 1表示执行OK,0表示有问题
     *
     * @throws ExecuteException
     * @throws IOException
     *             会抛异常,原因,进程可能还没有退出 会发生异常,6秒前ping命令已经结束,ExecuteWatchdog相当于无效
     *             但进程还没有退出,直接到exitValue会出现异常
     */
    @Test
    public void test4() throws ExecuteException, IOException {
        String line = "AcroRd32.exe /p /h \"" + file.getAbsolutePath() + "\"";
        line = "ping   www.baidu.com";
        CommandLine cmdLine = CommandLine.parse(line);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(1);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(6 * 1000);
        executor.setWatchdog(watchdog);
        int exitValue = executor.execute(cmdLine);
        System.out.println(exitValue);
    }

    @Test
    public void test5() throws ExecuteException, IOException {
        Map map = new HashMap();
        map.put("file", new File("invoice.pdf"));
        CommandLine cmdLine = new CommandLine("AcroRd32.exe");
        cmdLine.addArgument("/p");
        cmdLine.addArgument("/h");
        cmdLine.addArgument("${file}");
        cmdLine.setSubstitutionMap(map);
        DefaultExecutor executor = new DefaultExecutor();
        executor.setExitValue(1);
        ExecuteWatchdog watchdog = new ExecuteWatchdog(60000);
        executor.setWatchdog(watchdog);
        int exitValue = executor.execute(cmdLine);

    }

    @Test
    public void test6() throws ExecuteException, IOException,
            InterruptedException {
        /*
         * CommandLine cmdLine = new CommandLine("AcroRd32.exe");
         * cmdLine.addArgument("/p"); cmdLine.addArgument("/h");
         * cmdLine.addArgument("${file}"); HashMap map = new HashMap();
         * map.put("file", new File("invoice.pdf"));
         * cmdLine.setSubstitutionMap(map);
         */
        String line = "ping -t www.baidu.com";
        CommandLine cmdLine = CommandLine.parse(line);
        DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
        ExecuteWatchdog watchdog = new ExecuteWatchdog(10 * 1000);
        Executor executor = new DefaultExecutor();
        executor.setExitValue(1);
        executor.setWatchdog(watchdog);
        executor.execute(cmdLine, resultHandler);

        // some time later the result handler callback was invoked so we
        // can safely request the exit value

        resultHandler.waitFor();
        System.out.println(resultHandler.getExitValue());

    }

    @Test
    public void test7() {

    }
}


0 0
原创粉丝点击