Skip to content

Commit a8509f0

Browse files
authored
run code from java process
1 parent 5343b2f commit a8509f0

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

process-execute/read.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
exeucte command line application and get the result ( with pipes )
2+
```sh
3+
ProcessBuilder pb = new ProcessBuilder();
4+
pb.command("/bin/sh", "-c", String.format("cat %s | grep %s | head -n 1", csvPath, occurence));
5+
// !!! do not use without "/bin/sh" !!!!
6+
Process proc = pb.start();
7+
try {
8+
proc.waitFor(10, TimeUnit.SECONDS);
9+
} catch (InterruptedException e) {
10+
throw new IOException(e.getMessage());
11+
}
12+
13+
String responseLine = "";
14+
try (BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()))) {
15+
responseLine = reader.readLine();
16+
}
17+
// no result has found
18+
if(responseLine == null){
19+
return StringUtils.EMPTY;
20+
}
21+
22+
```
23+
24+
[example](https://github.com/zeroturnaround/zt-exec)
25+
```java
26+
result = new ProcessExecutor().commandSplit(commandLine).start().getFuture().get();
27+
if(result.getExitValue()!=0){
28+
throw new IllegalStateException("result of command line execution is not 0: "+result.getExitValue());
29+
}
30+
31+
List<String> output = new ProcessExecutor().commandSplit(commandLine).execute().getOutput().getLines();
32+
33+
```
34+
35+
http://commons.apache.org/proper/commons-exec/tutorial.html

process-execute/read.me

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)