title: JSch - How do I find out if a session is already opened?
body: <p>I have a runnable that makes an SSH connection and transfers a file from server to local. The problem is that I can't correctly figure out how to tell if there is already an active session going on. Every time I run the below code the value of getSession() is null. I can't figure out where I am going wrong and I've been staring at this code for the last several hours. Any help is appreciated.</p>
  
  <ul>
  <li>On a side note, I also need to figure out how to close a connection from another class (i.e. MenuItem in Main Activity that a user clicks to close the connection). Since I can't even figure out how to get the current session of JSch, it is a bit outside my reach. Any help on this would also be appreciated.</li>
  </ul>
  
  <p>My code is as follows:</p>
  
  <pre><code>import com.jcraft.jsch.Channel;
  import com.jcraft.jsch.ChannelSftp;
  import com.jcraft.jsch.JSch;
  import com.jcraft.jsch.JSchException;
  import com.jcraft.jsch.Session;
  import com.jcraft.jsch.SftpException;
  
  public class SCPReceive implements Runnable {
  
  private JSch jsch;
  private ChannelSftp sftpChannel;
  private Session session;
  
  private String username;
  private String password;
  private String host;
  private int port;
  
  private volatile static SCPReceive instance = null;
  
  SCPReceive() {
  
      this.jsch = new JSch();
      this.username = "username";
      this.password = "password";
      this.host = "server.com";
      this.port = 22;
  
  
  }
  
  
  public static SCPReceive getInstance() {
  
      if (SCPReceive.instance == null) {
  
          synchronized (SCPReceive.class) {
  
              if (SCPReceive.instance == null) {
  
                  SCPReceive.instance = new SCPReceive();
  
              }
  
          }
  
      }
  
      return SCPReceive.instance;
  
  }
  
  public void run() {
  
  
      if (null == getSession() || !getSession().isConnected()) {
  
          try {
          System.out.println("Opening SFTP Connection");
          setSession(jsch.getSession(this.username, this.host, this.port));
          getSession().setConfig("StrictHostKeyChecking", "no");
          getSession().setPassword(this.password);
          getSession().connect();
  
          Channel channel = getSession().openChannel("sftp");
          channel.connect();
          setSftpChannel((ChannelSftp) channel);
          } catch(Exception e){
              System.out.println("Error on connecting to server: " + e);
          }
  
          try {
              get();
          } catch (SftpException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
  
      } else {
  
          System.out.println("SFTP Connection already open");
  
      }
  
      System.out.println("Success");
  
  }
  
  
  public void closeConnection() {
  
      System.out.println("Closing SFTP connection");
      getSftpChannel().exit();
      getSession().disconnect();
      System.out.println("SFTP Connection closed");
  
  }
  
  public boolean isOpen() {
  
      if (getSession().isConnected()) {
  
          return true;
  
      } else {
  
          return false;
  
      }
  
  }
  
  private ChannelSftp getSftpChannel() {
  
      return this.sftpChannel;
  
  }
  
  private Session getSession() {
  
      return this.session;
  
  } 
  
  private void setSftpChannel(ChannelSftp sftpChannel) {
  
      this.sftpChannel = sftpChannel;
  
  }
  
  private void setSession(Session session) {
  
      this.session = session;
  
  }
  
  
  public void get() throws SftpException {
      String remote = "/path/to/file/1.mp4";
      String local = "/save/path/to/f1le/1.mp4";
  
      System.out.println("Retrieving file: " + remote + " saving to: " + local);
      getSftpChannel().get(remote, local);
      System.out.println("Success");
  
  }
  
  }
  </code></pre>
  
  <p>Edit:
   The runnable is called through:</p>
  
  <pre><code>            @Override
          public void actionPerformed(ActionEvent e) {
              SCPReceive t = new SCPReceive();
              Thread SCPReceive_thread = new Thread(t);
              SCPReceive_thread.start();
  
          }
      });
  </code></pre>