【发布时间】:2016-09-28 10:33:55
【问题描述】:
我需要在使用 JSch 触发任何命令时收到错误消息。
目前我正在使用下面的代码来获取tail 命令的输出,但是如果文件不存在,我应该得到错误作为输出(我没有得到)。
public String getOutput() {
LOGGER.debug("[getOutput]");
StringBuffer output = new StringBuffer();
InputStream in = null;
if (channel != null && channel.isConnected()) {
try {
in = channel.getInputStream();
byte[] tmp = new byte[1024];
while (true) {
LOGGER.debug("[getOutput] in while");
while (in.available() > 0) {
LOGGER.debug(in.available());
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
output.append(new String(tmp, 0, i));
}
if (channel.isClosed()) {
LOGGER.debug("[getOutput] Channel is closed, so breaking while loop");
break;
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
channel.disconnect();
}
} else {
System.out.println("Channel is disconnected");
}
return output.toString();
}
有什么方法可以让我也收到错误消息吗?
提前致谢。
【问题讨论】: