Java 堆栈跟踪打印到System.out,但不是System.err
我使用的这个类(JSch)我并不完全理解,但我对它的工作原理有一个粗略的了解。如果exitStatus不是我想要的,我试图抛出一个异常。然而,使用Java 堆栈跟踪打印到System.out,但不是System.err,java,printstacktrace,Java,Printstacktrace,我使用的这个类(JSch)我并不完全理解,但我对它的工作原理有一个粗略的了解。如果exitStatus不是我想要的,我试图抛出一个异常。然而,使用 e.printStackTrace() or e.printStackTrace(System.err) //does nothing e.printStackTrace(System.out) //prints the whole stack trace. 我以前从未见过这个问题,所以我想知道是不是我不了解的API导致了这个问题,还是异常堆栈跟
e.printStackTrace() or e.printStackTrace(System.err) //does nothing
e.printStackTrace(System.out) //prints the whole stack trace.
我以前从未见过这个问题,所以我想知道是不是我不了解的API导致了这个问题,还是异常堆栈跟踪的工作方式不同?我有一种感觉,使用ApacheLog4J可以工作,但是我太懒了,不能仅仅为了测试目的而设置它,而且它不能回答为什么会发生这种情况的困惑
如果感兴趣,下面是代码(我想在exitStatus!=0时引发异常):
Channel=null;
int exitStatus=1;
System.out.println(命令);
试一试{
channel=session.openChannel(“exec”);
ChannelExec ChannelExec=(ChannelExec)通道;
channelExec.setPty(真);
channelExec.setCommand(命令);
channelExec.setInputStream(空);
channelExec.setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
字节[]tmp=新字节[1024];
int超时=0;
while(true){
while(in.available()>0){
timeout=0;//如果读取成功,则重置超时
inti=in.read(tmp,0,1024);
如果(i<0)断裂;
字符串状态=新字符串(tmp,0,i);
/*系统输出打印项次(状态)*/
if(status.toLowerCase().包含(“不正确的密码”)){
通道断开();
}else if(status.toLowerCase()包含(“密码”)){
OutputStream out=channel.getOutputStream();
out.write(args);
out.write('\n');
out.close();
}
}
if(channel.isClosed()){
exitStatus=channel.getExitStatus();
打破
}
尝试{Thread.sleep(1000);}catch(异常ee){ee.printStackTrace();}
//4圈内无任何内容读取,但流未关闭,可能正在等待用户输入,所以退出
如果(超时+++>5){
打破
}
}
}捕获(ioe异常ioe){
抛出新的ApplicationException(ioe);
}捕获(JSCHEException je){
抛出新的ApplicationException(“无法为SSH创建exec通道。”,je);
}最后{
如果(channel.isConnected())channel.disconnected();
}
//如果(exitStatus!=0)抛出新的MyCustomException(errormsg)
谢谢。您在问为什么
//如果(exitStatus!=0)
不起作用?我不明白你的问题。您要在哪里打印堆栈跟踪?如果您想在exitStatus不是0时引发异常,是什么阻止您这样做的?是否显示任何打印到System.err的内容?也许您已将stderr重定向到文件或/dev/null。@KlitosKyriacou谢谢,您的评论为我的问题提供了一个提示。问题是第10行的这行代码:channelExec.setErrStream(System.err)代码>
Channel channel = null;
int exitStatus = 1;
System.out.println(command);
try {
channel = session.openChannel("exec");
ChannelExec channelExec = (ChannelExec) channel;
channelExec.setPty(true);
channelExec.setCommand(command);
channelExec.setInputStream(null);
channelExec.setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
int timeout = 0;
while (true){
while (in.available() > 0) {
timeout = 0; // if read success, reset timeout
int i = in.read(tmp, 0, 1024);
if (i < 0)break;
String status = new String(tmp, 0, i);
/*System.out.println(status);*/
if (status.toLowerCase().contains("incorrect password")) {
channel.disconnect();
} else if (status.toLowerCase().contains("password")) {
OutputStream out = channel.getOutputStream();
out.write(args);
out.write('\n');
out.close();
}
}
if (channel.isClosed()){
exitStatus = channel.getExitStatus();
break;
}
try { Thread.sleep(1000); } catch (Exception ee) { ee.printStackTrace(); }
// nothing reads for 4 turns but stream is not closing, probably waiting for user input so exit
if (timeout++ > 5) {
break;
}
}
} catch (IOException ioe) {
throw new ApplicationException(ioe);
} catch (JSchException je) {
throw new ApplicationException("Could not create an exec channel for SSH. ", je);
} finally {
if (channel.isConnected()) channel.disconnect();
}
//if (exitStatus != 0) throw new MyCustomException(errormsg)