日志输出显示了运行的命令,可以看到其中包括一个 bash 命令。thufir@dur:~/NetBeansProjects/threadedExecTelnet$
thufir@dur:~/NetBeansProjects/threadedExecTelnet$ gradle run
> Task :run
May 19, 2020 12:25:47 P.M. net.bounceme.dur.exec.ExecList execute
INFO: [bash, -c, ls /home/thufir/NetBeansProjects/threadedExecTelnet]
build
build.gradle
gradle
gradlew
gradlew.bat
LICENSE
README.md
settings.gradle
src
BUILD SUCCESSFUL in 859ms
3 actionable tasks: 1 executed, 2 up-to-date
thufir@dur:~/NetBeansProjects/threadedExecTelnet$
虽然我并不太在意那些繁琐而啰嗦的代码public void execute() throws IOException {
log.info(listToExec.toString());
String[] arrayToExec = listToExec.toArray(new String[0]);
Process process = Runtime.getRuntime().exec(arrayToExec);
InputStream inputStream = process.getInputStream();
Reader inputStreamReader = new InputStreamReader(inputStream);
try (BufferedReader bufferedReader = new BufferedReader(inputStreamReader)) {
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
System.out.flush();
}
}
}
但我不明白为什么有时会出现这样的情况: bash 是必需的,其他时候则不需要。