Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mhashim6/system-command-executor
A library to make the process of executing system commands through java a simple task.
https://github.com/mhashim6/system-command-executor
beginner-friendly command-line-tools java library object-oriented oop
Last synced: 3 months ago
JSON representation
A library to make the process of executing system commands through java a simple task.
- Host: GitHub
- URL: https://github.com/mhashim6/system-command-executor
- Owner: mhashim6
- License: mit
- Created: 2016-12-22T19:52:30.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2022-07-31T12:50:48.000Z (over 2 years ago)
- Last Synced: 2024-05-02T04:19:30.394Z (9 months ago)
- Topics: beginner-friendly, command-line-tools, java, library, object-oriented, oop
- Language: Java
- Homepage:
- Size: 56.6 KB
- Stars: 24
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
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();
----------