[java] "com.jcraft.jsch.JSchException: Auth fail" with working passwords

While trying to upload the file to our server, i am getting the following exception

    com.jcraft.jsch.JSchException: Auth fail
        at com.jcraft.jsch.Session.connect(Session.java:464)
        at com.jcraft.jsch.Session.connect(Session.java:158)
        at FtpService.transferFileToReciever(FtpService.java:80)
        at FtpService.transferFileToReciever(FtpService.java:54)
        at FtpService.transferFileToRecievers(FtpService.java:44)
        at FtpService.transferSingeFile(FtpService.java:241)
        at FtpService.main(FtpService.java:26)
    Auth fail

The part of function transferFileToReciever from source file is

        JSch jsch = new JSch();
        Session session = jsch.getSession(username, host, 22);

        session.connect(); //geting exception here

        boolean ptimestamp = true;

The passwords are working, since i can do login using ssh, but using JSCh it doesnt work even provided with key, username and password. Using id_dsa key with java version "1.6.0_25". What could be the error?

Found other similar question, but not the answer. Thanks in advance.

This question is related to java jsch

The answer is

I have also face the Auth Fail issue, the problem with my code is that I have


It changed it to


Then it works.

Example case, when I get file from remote server and save it in local machine
package connector;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

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 Main {

    public static void main(String[] args) throws JSchException, SftpException, IOException {
        // TODO Auto-generated method stub
        String username = "XXXXXX";
        String host = "XXXXXX";
        String passwd = "XXXXXX";
        JSch conn = new JSch();
        Session session = null;
        session = conn.getSession(username, host, 22);
        session.setConfig("StrictHostKeyChecking", "no");

        ChannelSftp channel = null;
        channel = (ChannelSftp)session.openChannel("sftp");


        InputStream in = channel.get("testScp");
        String lf = "OBJECT_FILE";
        FileOutputStream tergetFile = new FileOutputStream(lf);

        int c;
        while ( (c= in.read()) != -1 ) {





in my case I was using below dependency


and getting the same exception of Auth fail, but updated dependency to below version and problem get resolved.


Found other similar question, but not the answer.

It would have been interesting to know, where you have found this question.

As far as I can remember and according com.jcraft.jsch.JSchException: Auth cancel try to add to method .addIdentity() a passphrase. You can use "" in case you generated a keyfile without one. Another source of error is the fingerprint string. If it doesn't match you will get an authentication failure either (depends from on the target server).

And at last here my working source code - after I could solve the ugly administration tasks:

public void connect(String host, int port, 
                    String user, String pwd,
                    String privateKey, String fingerPrint,
                    String passPhrase
                  ) throws JSchException{
    JSch jsch = new JSch();

    String absoluteFilePathPrivatekey = "./";
    File tmpFileObject = new File(privateKey);
    if (tmpFileObject.exists() && tmpFileObject.isFile())
      absoluteFilePathPrivatekey = tmpFileObject.getAbsolutePath();

    jsch.addIdentity(absoluteFilePathPrivatekey, passPhrase);
    session = jsch.getSession(user, host, port);

    //Password and fingerprint will be given via UserInfo interface.
    UserInfo ui = new UserInfoImpl(pwd, fingerPrint);


    Channel channel = session.openChannel("sftp");
    c = (ChannelSftp) channel;

Try to add auth method explicitly as below, because sometimes it is required:

session.setConfig("PreferredAuthentications", "password");

If username/password contains any special characters then inside the camel configuration use RAW for Configuring the values like

  • RAW(se+re?t&23) where se+re?t&23 is actual password

  • RAW({abc.ftp.password}) where {abc.ftp.password} values comes from a spring property file.

By using RAW, solved my issue.