title: Secure FTP using private key authentication in java
body: <pre><code>import com.jcraft.jsch.*;
  import com.jcraft.jsch.JSchException;
  import oracle.jdbc.driver.OracleDriver;
  import java.io.*;
  import java.util.*;
  import java.sql.*;
  import java.net.*;
  
  public class SecureFTP {
  
  public static void main(String[] args) throws IOException , ClassNotFoundException, JSchException, SftpException{
  
  
  
      JSch jsch = new JSch();
      File file = new File("/home/xxxxx/.ssh/id_rsa");
      Session session = null;
      URL keyFileURL = null;
      URI keyFileURI = null;
      if (file.exists())
      {
          keyFileURL = file.toURL();
          if (keyFileURL == null)
          {
              System.out.println("what");
              throw new RuntimeException("Key file not found in classpath");
          }
      }
      else System.out.println("FIle not found");
      try{
               keyFileURI = keyFileURL.toURI();
      }
      catch(Exception URISyntaxException)
      {
          System.out.println("Wrong URL");
  }
  
  
      String privateKey = ".ssh/id_rsa";
  
      //jsch.addIdentity(privateKey);
  
      jsch.addIdentity(new File(keyFileURI).getAbsolutePath());
      System.out.println(new File(keyFileURI).getAbsolutePath() + " LOL");
      session = jsch.getSession("username", "servername");
      //session.setPassword("password");
  
      java.util.Properties config = new java.util.Properties();
      config.put("StrictHostKeyChecking", "no");
      session.setConfig(config);
  
      // connect
      session.connect();
  
      // get SFTP channel
      Channel channel = session.openChannel("sftp");
      channel.connect();
      ChannelSftp schannel = (ChannelSftp) channel;
  
      FileInputStream fis = new FileInputStream(sourcefile);
      schannel.put(fis, destinationfile );
      fis.close();
  
  
  
  }
  
      schannel.exit();
      session.disconnect();
  
  }
  
  }
  </code></pre>
  
  <p>As you can see from the code I have commented out, I have tried everything possible to get this working and the only thing that works is if I set the password directly. I am trying to use the RSA private key generated, but I keep getting an auth fail. </p>
  
  <p>I have added the public key to the list of authorized keys on the target server. And there is no passphrase.</p>
  
  <p>Is there something else I am supposed to do? Like say, while generating the keys? Is there a step I am missing? </p>
  
  <p>Is there another library I can use to implement the same function?</p>