Simple Library to make the process of executing system commands through java a simple task.
The library is thread-safe, can be used to execute multiple commands asynchronously.
------
**How to**
If you want to execute a command, and redirect the output to the `console`:
try {
CommandExecutor.execute("ping google.com");
}
catch (IOException e) {
e.printStackTrace();
}
------
However, this library gives you more control over the commands being executed;
- You can use the `CommandBuilder` as *wrapper* for the parts that form the command line you
want to execute; such as arguments
(`CommandBuilder#withArgs`) and
options(`CommandBuilder#withOptions`).
- You can *redirect* the `standard` and `error` outputs to any object You want, just *implement* the `Appender` interface.
- You can *retrieve* the `exit code` of the process, or *abort* the process,
by simply using the `ProcessMonitor` and `ExecutionReport` objects.
------
An advanced example:
Command cmd = new CommandBuilder().forCommandLine("ping").withArgs("google.com").build();
ExecutionOutputPrinter eop = new ExecutionOutputPrinter(new Appender() {
@Override
public void appendStdText(String text) {
// your code to show std output lines.
}
@Override
public void appendErrText(String text) {
// your code to show error lines.
}
});
try {
ProcessMonitor pMonitor = CommandExecutor.execute(cmd, null, eop); //execute the command, redirect the output to eop.
ExecutionReport report = pMonitor.getExecutionReport(); //blocks until the process finishes or gets aborted.
String commandLine = cmd.string();
int exitCode = report.exitValue();
System.out.printf("command line: %s\nexecution finished with exit code: %d\n\n", commandLine, exitCode);
}
catch (UnrecognisedCmdException e) {
System.err.println(e);
}
to abort a running process (command), you can use the `ProcessMonitor` instance that you obtained previously:
pMonitor.abort();
----------
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。 毕业设计、课程设计源码文件,已经过测试可以直接使用。
资源推荐
资源详情
资源评论
收起资源包目录
毕业设计&课设-一个库,使通过java执行系统命令的过程成为一个简单的任务。.zip (17个子文件)
System-Command-Executor-master
System-Command-Executor.iml 423B
.github
FUNDING.yml 812B
src
mhashim6
commander
exceptions
UnrecognisedCmdException.java 484B
Sample.java 2KB
main
ProcessMonitor.java 1KB
ExecutionOutputPrinter.java 2KB
ExecutionReport.java 513B
CommandBuilder.java 4KB
Appender.java 250B
Command.java 142B
CommandExecutor.java 3KB
.idea
artifacts
System_Command_Executor_jar.xml 346B
vcs.xml 167B
misc.xml 258B
modules.xml 286B
.gitignore 45B
README.md 2KB
共 17 条
- 1
资源评论
zy_zeros
- 粉丝: 953
- 资源: 320
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功