下面的代码在控制台中执行 karaf 命令。这在 karaf 版本 3.0.3 中运行良好。它在 4.0.0 或更高版本中失败
@Inject
CommandProcessor commandProcessor;
private class dummyCallable implements Callable{
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
final PrintStream printStream = new PrintStream(byteArrayOutputStream);
final CommandSession commandSession = commandProcessor.createSession(System.in, printStream, System.err);
String[] commands;
dummyCallable( String[] commands){
this.commands = commands;
}
public String call() {
try {
for(String command:commands) {
System.err.println(command);
commandSession.execute(command);
}
} catch (Exception e) {
e.printStackTrace(System.err);
}
return byteArrayOutputStream.toString();
}
}
protected String executeCommands(final String ...commands) {
String response;
dummyCallable dd = new dummyCallable(commands);
ExecutorService executor = Executors.newFixedThreadPool(1);
FutureTask<String> futureTask2 = new FutureTask<String>(dd);
try {
executor.submit(futureTask2);
response = futureTask2.get();
} catch (Exception e) {
e.printStackTrace(System.err);
response = "SHELL COMMAND TIMED OUT: ";
}
return response;
}
任何人都可以为我提供一个可以使用 karaf 4.0.0 的代码吗