JSch session.openChannel

问题描述 投票:-1回答:1

我有这个代码,当我运行它时,显示调试语句'EXEC模式中的开放通道'但openChannel(“exec”)失败但似乎没有抛出异常,因为方法中没有捕获异常但是出现在调用方法为没有消息的空异常

private int runCommand( Session session, String command ) throws RemoteAccessException {
    log.debug("Execute command [" + command + "]");
    int status = SUCCESS_STATUS;
    ChannelExec channel = null;
    try {
        if ( session != null ) {
            log.debug("Open channel in EXEC mode ");
            channel = (ChannelExec)session.openChannel("exec");

            log.debug("Set the command into Channel to be executed");
            channel.setCommand(command);
            channel.setErrStream( System.err);

            log.debug("Set the stream to read result from command");
            InputStream inStream = channel.getInputStream();

            log.debug("Connect the channel which executes the command ");
            channel.connect();
            log.debug("Read return from command");
            while ( true ) {
                byte[] inBytes = new byte[2048];
                while ( inStream.available() > 0) {
                    int i = inStream.read( inBytes, 0 ,2048);
                    if ( i < 0 ) {
                        break;
                    }
                    log.info( new String( inBytes, 0, i));
                }
                if ( channel.isClosed()) {          
                    status = channel.getExitStatus();
                    log.debug("Command completed with exit status [" + status + "]");

                    break;
                }
                Thread.sleep( SLEEP_INTERVAL);
            }   
        }
        else {
            throw new RemoteAccessException( "Unable to run a command as the Session has not been created");
        }
    }
    catch ( IOException ioe ) {
        throw new RemoteAccessException("Remote Command failed: " + ioe.getMessage());
    }
    catch ( JSchException je ) {
        throw new RemoteAccessException("Remote Command failed: " + je.getMessage());
    }
    catch ( InterruptedException ie ) {
        throw new RemoteAccessException("Remote Command has been interrupted");
    }
    catch ( Exception e ) {
        log.debug("RunCommand failure: " + e.getMessage());
    }
    finally {
        channel.disconnect();
    }
    return status;
}
jsch
1个回答
-1
投票

很抱歉迟到但我现在已经解决了代码中其他地方正在创建的问题

© www.soinside.com 2019 - 2024. All rights reserved.