Connect SFTP server using JSch

JSch is a Java library implementing SSH2 protocol, which allows us to connect and manipulate files on an SFTP server. In this tutorial, I will guide you all how to connect to an SFTP server using this library.

First, I will create a Maven project as an example:

Connect SFTP server using JSch

JSch dependency is as follows:

To connect to an SFTP server, we first need to initialize a JSch object:

Any connection between the client and server requires a session. We can create a Session for our connection from the JSch object which we just created:


  • login is the name of the SFTP user.
  • hostname is the address of the SFTP server.
  • The password is the SFTP user’s password.

Now, if you run the above code you will get the following error:

The reason is that for SSH connections we need to confirm that the server we are connecting to, is secure. To bypass this validation, we can modify the default configuration of a Session.

Run again, you will not get the error again.

Now from this Session object, we can open an SFTP channel to manipulate files on the SFTP server.

By default, when opening a ChannelSftp, the default folder is the user directory.

We can get the current directory by calling:

The entire code is as follows:

3.7/5 - (3 votes)

One thought on “Connect SFTP server using JSch

Add Comment