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() {
}
}
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
- common-exec调用进程命令
- linux进程控制-exec系列 exec系统调用
- 调用exec()后进程在何处终止
- TCL学习之子进程调用--open&exec
- Linux---之子父进程,及exec命令
- Runtime.exec 调用OS命令特例
- 使用java Runtime exec调用ping命令
- go语言exec包调用shell命令
- java进程调用外部程序时fork()+exec()分析
- linux的子进程调用exec( )系列函数
- 第三章 进程控制 3.1.3 exec系统调用2
- 第三章 进程控制 3.1.3 exec系统调用3
- fork函数创建子进程,exec函数执行命令
- Timer定时通过Runtime的exec方法调用命令“cmd exp..” 后,任务管理器不停的出现“Java.exe”和“cmd.exe”进程
- PHP中调用系统命令 system(),exec(),passthru()
- PHP中exec、system等函数调用linux命令问题
- PHP中调用系统命令的system(),exec(),passthru()
- php中使用exec,system等函数调用系统命令
- error LNK2019: 无法解析的外部符号,VS2010中可能的原因
- Android 弹出窗体
- appearance 设置公共属性
- URAL1785解题报告
- android面试题2
- common-exec调用进程命令
- iOS开发:开发者账号申请后真机调试详细步骤
- tbschedule 前后置处理器、定期执行任务
- .NET 控件的许可验证方案
- 黑马程序员————OC语言BLOCK和Protocol总结
- 原始套接字透析之实现sniffer
- Java初学习 - 多态和实现接口的类的对象赋值给接口引用的方法
- 搜集关于sip协议的几个网址
- java进程调用 Process