What is an example of using an SSH library in Java, and how can I establish a secure connection with it?

I’m looking for a simple example that demonstrates how Java SSH clients can be used to connect to a remote server. What libraries are commonly used for this, and how do I implement a basic SSH connection in Java?

Hey, if you’re diving into Java SSH clients and need to establish a secure connection with a remote server — whether it’s for running commands, file transfers, or automation — there are a few solid options to make this easier. One of the most popular ones is JSch (Java Secure Channel). It’s lightweight, simple to use, and works well for most tasks like command execution and SFTP.

:wrench: Basic Example with JSch:

import com.jcraft.jsch.*;

public class SSHExample {
    public static void main(String[] args) {
        String user = "your-username";
        String host = "remote.server.com";
        int port = 22;

        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(user, host, port);
            session.setPassword("your-password");

            // Avoid asking for key confirmation
            session.setConfig("StrictHostKeyChecking", "no");

            session.connect();
            System.out.println("Connected via SSH!");

            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand("ls -l");
            channel.setErrStream(System.err);
            channel.setInputStream(null);
            channel.connect();

            // Read output here if needed...

            channel.disconnect();
            session.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

:speech_balloon: Why use this? It’s quick to set up and great for simple tasks like executing commands. Plus, it’s got built-in support for file transfers (SFTP) too, making it a solid choice for a variety of use cases.

You’re on the right track with Java SSH clients! If you need something with more control and are looking to build more advanced SSH handling — for instance, SSH servers or complex client-side logic — you should check out Apache MINA SSHD. It’s more flexible than JSch, though it’s a bit more verbose to set up.

:wrench: How to use it:

You’ll need to add the dependency to your project:

<dependency>
  <groupId>org.apache.sshd</groupId>
  <artifactId>sshd-core</artifactId>
  <version>2.11.0</version>
</dependency>

Then you can set up your SSH client or server with custom configurations. It’s great when you need to do things like tunneling or implement custom authentication, where JSch’s simplicity may fall short.

:speech_balloon: Why use this? Apache MINA SSHD is industrial-strength and provides much more flexibility. It’s ideal for projects where Java SSH clients need more custom configurations or advanced SSH handling.

If you want something that’s a little more modern and user-friendly than JSch, you should definitely give sshj a try. It’s clean, supports OpenSSH formats, and is generally easier to read and maintain.

:wrench: Sample Connection with sshj:

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;

public class SSHJExample {
    public static void main(String[] args) {
        try {
            SSHClient ssh = new SSHClient();
            ssh.addHostKeyVerifier(new PromiscuousVerifier());
            ssh.connect("remote.server.com");
            ssh.authPassword("your-username", "your-password");

            Session session = ssh.startSession();
            try (Session.Command cmd = session.exec("ls -l")) {
                System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
                cmd.join();
            }

            ssh.disconnect();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

:speech_balloon: Why use this? It’s intuitive, modern, and more secure by default compared to some older libraries. It’s especially nice if you’re working in newer Java environments. Plus, it’s just easier to maintain.