title: SFTP with java client when keys are shared
body: <p>I have two servers A and B.
  I want to SFTP a file from server A to B.</p>
  
  <p>Public key of server A (<code>~/.ssh/id_rsa.pub</code>) has been added to the <code>~/.ssh/authorized_keys</code> of server B.</p>
  
  <p>From command line, I can SFTP from server A to B without entering password.
  However, from a <code>Java</code> client using library <a href="http://www.jcraft.com/jsch/" rel="nofollow noreferrer">Jsch</a> I am unable to make SFTP connection to server B and I am getting authentication error:</p>
  
  <pre><code>Error occurred during SFTP. Auth fail
  com.jcraft.jsch.JSchException: Auth fail
      at com.jcraft.jsch.Session.connect(Session.java:519)
      at com.jcraft.jsch.Session.connect(Session.java:183)
      at Main.main(Main.java:15)
  </code></pre>
  
  <p>Is there a way I can connect to server B for SFTP purposes using <code>Java</code> client without specifying password?</p>
  
  <p>Below is my <code>Java</code> code for reference:</p>
  
  <pre><code>import com.jcraft.jsch.*;
  
  public class Main {
  
      public static void main(String[] args) {
  
          JSch jsch = new JSch();
          Session session = null;
  
          try {
              session = jsch.getSession("processor", "remoteserver.myorg.com", 22);
              session.setConfig("StrictHostKeyChecking", "no");
              System.out.println("Trying to connect...");
              session.connect();
              System.out.println("Connected successfully.");
  
              Channel channel = session.openChannel("sftp");
              channel.connect();
              ChannelSftp sftpChannel = (ChannelSftp) channel;
              System.out.println("Doing SFTP...");
              sftpChannel.put("/tmp/test.txt", "/some/remote/folder");
              System.out.println("Success");
              sftpChannel.exit();
              session.disconnect();
  
          } catch (JSchException | SftpException e) {
              System.err.println("Error occurred during SFTP. " + e.getMessage());
              e.printStackTrace();
          }
  
      }
  }
  </code></pre>