title: Why does an SFTP connection still exist after the JSCH Channel has been closed?
body: <p>When the code below has finished running, <code>netstat -a|grep sftp</code> shows an open SFTP connection. It also shows up as an open connection in JProfiler.</p>
  
  <p><code>channel.isConnected()</code> in the <em>finally</em> block prints <em>false</em>. Any ideas why the connections is not being closed as I'm at a loss?</p>
  
  <pre><code>public static void clean() {
      com.jcraft.jsch.ChannelSftp channel = null;
      try {
          channel = Helper.openNewTLSftpChannel();
          channel.connect();
          channel.cd(remoteFileDirectory);
  
          List&lt;ChannelSftp.LsEntry&gt; list = channel.ls("*." + fileType);
          for (ChannelSftp.LsEntry file : list) {
              String fileName = file.getFilename();
              DateTime fileDate = new DateTime(parseDateFromFileName(fileName));
  
              //if this file is older than the cutoff date, delete from the SFTP share
              if (fileDate.compareTo(cleanupCutoffdate) &lt; 0) {
                  channel.rm(fileName);
              }
          }
      } catch (Exception exception) {
          exception.printStackTrace();
      } finally {
          if (channel != null) {
              channel.disconnect();
              System.out.println(channel.isConnected());
          }
      }
  }
  </code></pre>
  
  <hr>
  
  <p>Adding <code>openNewTLSftpChannel()</code> below:</p>
  
  <pre><code>public static ChannelSftp openNewSftpChannel(String privateKeyFileName, String password, String username, String host, int port)
          throws ConfigurationErrorException {
  
      JSch jsch = new JSch();
      File sftpPrivateFile = new File(privateKeyFileName);
      Channel channel;
      try {
          if (!sftpPrivateFile.canRead()) {
              throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath());
          }
          jsch.addIdentity(sftpPrivateFile.getAbsolutePath(), password);
          Session session = jsch.getSession(username, host, port);
          java.util.Properties config = new java.util.Properties();
          config.put("StrictHostKeyChecking", "no");
          session.setConfig(config);
          session.connect();
          channel = session.openChannel("sftp");
      } catch (JSchException jschException) {
          throw new ConfigurationErrorException("File access error: " + sftpPrivateFile.getAbsolutePath());
      }
      return (ChannelSftp) channel;
  }
  </code></pre>