🌐
Jcraft
jcraft.com › jsch
JSch - Java Secure Channel
JSch is a pure Java implementation of SSH2. JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.
ChangeLog of JSch
It may not be started with "password:". - change: ChannelSftp#put(InputStream src, String dst) will not check if dst is directory or not, and if an exception is thrown, the check will be done. - change: if the compression is enabled without jzlib.jar, an exception will be thrown. - feature: JSch#a...
Java Secure Channel
Our Crafts WiredX WeirdX JSch JZlib JCTerm JOrbis JHttpTunnel JRexec · README ChangeLog · IETF secsh Examples JCTerm jsch-agent-proxy JSch for J2ME Wiki · JSch is supported by · StreamForwarding.java · Copyright 1998-2012 JCraft, Inc. All rights reserved.
🌐
Maven Repository
mvnrepository.com › artifact › com.jcraft › jsch
Maven Repository: com.jcraft » jsch
November 26, 2018 - Home » com.jcraft » jsch · JSch is a pure Java implementation of SSH2 · LicenseBSD · CategoriesSSH Libraries · Tagsnetworkshellsshremotesecure · Ranking · #33524in MvnRepository · #9in SSH Libraries · HomePage http://www.jcraft.com/jsch/ 🔍 Inspect URL ·
Discussions

What is the modern equivalent of Jsch (Library for SSH and SFTP)
Apache MINA SSHD Project - SSH & SFTP support https://mina.apache.org/sshd-project/ https://mvnrepository.com/artifact/org.apache.sshd/sshd-core - 2022 https://mvnrepository.com/artifact/org.apache.sshd/sshd-sftp - 2022 Apache SSHD is a 100% pure java library to support the SSH protocols on both the client and server side. This library is based on Apache MINA, a scalable and high performance asynchronous IO library. SSHD does not really aim at being a replacement for the SSH client or SSH server from Unix operating systems, but rather provides support for Java based applications requiring SSH support. Apache Commons VFS - SFTP and other filesystems https://mvnrepository.com/artifact/org.apache.commons/commons-vfs2 - 2021 https://commons.apache.org/proper/commons-vfs/ https://commons.apache.org/proper/commons-vfs/filesystems.html Note JSCH does have modern updates via forks https://mvnrepository.com/artifact/com.github.mwiede/jsch - updated 2022 https://github.com/mwiede/jsch More on reddit.com
🌐 r/javahelp
2
4
October 19, 2022
java - Run a command over SSH with JSch - Stack Overflow
I'm trying to run a command over SSH with JSch, but JSch has virtually no documentation and the examples I've found are terrible. For example, this one doesn't show code for handling the output str... More on stackoverflow.com
🌐 stackoverflow.com
Wat is het moderne equivalent van Jsch (Bibliotheek voor SSH en SFTP)

Apache MINA SSHD-project - SSH & SFTP-ondersteuning

https://mina.apache.org/sshd-project/

https://mvnrepository.com/artifact/org.apache.sshd/sshd-core - 2022

https://mvnrepository.com/artifact/org.apache.sshd/sshd-sftp - 2022

Apache SSHD is een 100% pure Java-bibliotheek om de SSH-protocollen te ondersteunen, zowel aan de client- als aan de serverzijde.
Deze bibliotheek is gebaseerd op Apache MINA,
een schaalbare en krachtige asynchrone IO-bibliotheek.
SSHD is niet echt bedoeld als vervanging
voor de SSH-client of SSH-server van Unix-besturingssystemen,
maar biedt ondersteuning voor Java-gebaseerde applicaties
die SSH-ondersteuning vereisen.

Apache Commons VFS - SFTP en andere bestandssystemen

https://mvnrepository.com/artifact/org.apache.commons/commons-vfs2 - 2021

https://commons.apache.org/proper/commons-vfs/

https://commons.apache.org/proper/commons-vfs/filesystems.html

Opmerking JSCH heeft moderne updates via forks

https://mvnrepository.com/artifact/com.github.mwiede/jsch - bijgewerkt 2022

https://github.com/mwiede/jsch

More on reddit.com
🌐 r/javahelp
2
4
October 19, 2022
jsch

It just sounds like Swaggersouls now

More on reddit.com
🌐 r/jschlatt
23
507
May 24, 2024
🌐
Epaul
epaul.github.io › jsch-documentation › simple.javadoc › com › jcraft › jsch › Channel.html
Channel (simple JSch API)
Sets the OutputStream for this channel. All data arriving in SSH_MSG_CHANNEL_DATA messages from the remote side will be written to this OutputStream. This method should be called before connect(). The stream will be closed on disconnect(com.jcraft.jsch.Session).
🌐
Reddit
reddit.com › r/javahelp › what is the modern equivalent of jsch (library for ssh and sftp)
r/javahelp on Reddit: What is the modern equivalent of Jsch (Library for SSH and SFTP)
October 19, 2022 -

It seems to be Abandonware. The last release I see on Maven Central is 0.55 on November 18th, 2018.

What is a good modern equivalent of this wonderful library?

Top answer
1 of 2
4
Apache MINA SSHD Project - SSH & SFTP support https://mina.apache.org/sshd-project/ https://mvnrepository.com/artifact/org.apache.sshd/sshd-core - 2022 https://mvnrepository.com/artifact/org.apache.sshd/sshd-sftp - 2022 Apache SSHD is a 100% pure java library to support the SSH protocols on both the client and server side. This library is based on Apache MINA, a scalable and high performance asynchronous IO library. SSHD does not really aim at being a replacement for the SSH client or SSH server from Unix operating systems, but rather provides support for Java based applications requiring SSH support. Apache Commons VFS - SFTP and other filesystems https://mvnrepository.com/artifact/org.apache.commons/commons-vfs2 - 2021 https://commons.apache.org/proper/commons-vfs/ https://commons.apache.org/proper/commons-vfs/filesystems.html Note JSCH does have modern updates via forks https://mvnrepository.com/artifact/com.github.mwiede/jsch - updated 2022 https://github.com/mwiede/jsch
2 of 2
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
Javadoc.io
javadoc.io › doc › com.jcraft › jsch › latest › index.html
jsch 0.1.55 javadoc (com.jcraft)
Latest version of com.jcraft:jsch · https://javadoc.io/doc/com.jcraft/jsch · Current version 0.1.55 · https://javadoc.io/doc/com.jcraft/jsch/0.1.55 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/com.jcraft/jsch/0.1.55/package-list ·
🌐
Baeldung
baeldung.com › home › java › java io › java jsch library to read remote file line by line
Java JSch Library to Read Remote File Line by Line | Baeldung
October 8, 2024 - The Java Secure Channel (JSch) library provides an API for connecting Java applications to remote servers, enabling various remote operations.
🌐
GitHub
github.com › mwiede › jsch
GitHub - mwiede/jsch: fork of the popular jsch library · GitHub
fork of the popular jsch library. Contribute to mwiede/jsch development by creating an account on GitHub.
Starred by 1K users
Forked by 184 users
Languages   Java
Find elsewhere
🌐
Jenkins
plugins.jenkins.io › jsch
JSch dependency | Jenkins plugin
February 25, 2025 - Jenkins plugin that brings the JSch library as a plugin dependency, and provides an SSHAuthenticatorFactory for using JSch with the ssh-credentials plugin.
🌐
SourceForge
sourceforge.net › projects › jsch
JSch download | SourceForge.net
Download JSch for free. JSch is a pure Java implementation of SSH2. JSch allows you to connect to an sshd server and use port forwarding, X11 forwarding, file transfer, etc., and you can integrate its functionality into your own Java programs.
🌐
Maven Repository
mvnrepository.com › artifact › com.jcraft › jsch › 0.1.55
Maven Repository: com.jcraft » jsch » 0.1.55
November 26, 2018 - HomePage http://www.jcraft.com/jsch/ 🔍 Inspect URL
Published   Nov 26, 2018
Version   0.1.55
🌐
Javadoc.io
javadoc.io › doc › com.jcraft › jsch › 0.1.54 › com › jcraft › jsch › JSch.html
JSch - jsch 0.1.54 javadoc
Latest version of com.jcraft:jsch · https://javadoc.io/doc/com.jcraft/jsch · Current version 0.1.54 · https://javadoc.io/doc/com.jcraft/jsch/0.1.54 · package-list path (used for javadoc generation -link option) https://javadoc.io/doc/com.jcraft/jsch/0.1.54/package-list ·
🌐
Cubicpower
cubicpower.idv.tw › BigData › Glossary › java › j › jsch.htm
CubicPower-java Programming Glossary: jsch
Java: What is the best way to SFTP a file from a server · http://stackoverflow.com/questions/14617/java-what-is-the-best-way-to-sftp-a-file-from-a-server
🌐
Mkyong
mkyong.com › home › java › file transfer using sftp in java (jsch)
File Transfer using SFTP in Java (JSch) - Mkyong.com
October 1, 2020 - package com.mkyong.io.howto; import com.jcraft.jsch.*; public class SFTPFileTransfer { private static final String REMOTE_HOST = "1.2.3.4"; private static final String USERNAME = ""; private static final String PASSWORD = ""; private static final int REMOTE_PORT = 22; private static final int SESSION_TIMEOUT = 10000; private static final int CHANNEL_TIMEOUT = 5000; public static void main(String[] args) { String localFile = "/home/mkyong/local/random.txt"; String remoteFile = "/home/mkyong/remote/afile.txt"; Session jschSession = null; try { JSch jsch = new JSch(); jsch.setKnownHosts("/home/mk
🌐
Medium
jxausea.medium.com › spring-boot-integrated-jsch-quick-start-demo-3e02e90a5eb5
Spring Boot integrated JSch quick start demo | by Harries | Medium
March 25, 2024 - Spring Boot integrated JSch quick start demo 1.JSch introduction JSch is a pure Java implementation of SSH2. JSch allows you to connect to sshd servers and use port forwarding, X11 forwarding, file …
🌐
SAP
userapps.support.sap.com › sap › support › knowledge › en › 3518866
3518866 - What is the JSCH version used by my SFTP Adapter | SAP Knowledge Base Article
Jsch Version , Jsch, SFTP, SFTP Adapter, SP level,, PIB2BSFTP, SP04 ,Jsch 0.1.51, Jsch 0.1.53, 2337525 , Jsch library, Jsch 0.1.53, 1.55, Jsch 0.1.55 , mwiede/jsch 0.2.8, 3335633 , KBA , BC-XI-CON-SFT , Secure File Transfer Protocol Adapter , How To
Top answer
1 of 9
107

The following code example written in Java will allow you to execute any command on a foreign computer through SSH from within a java program. You will need to include the com.jcraft.jsch jar file.

  /* 
  * SSHManager
  * 
  * @author cabbott
  * @version 1.0
  */
  package cabbott.net;

  import com.jcraft.jsch.*;
  import java.io.IOException;
  import java.io.InputStream;
  import java.util.logging.Level;
  import java.util.logging.Logger;

  public class SSHManager
  {
  private static final Logger LOGGER = 
      Logger.getLogger(SSHManager.class.getName());
  private JSch jschSSHChannel;
  private String strUserName;
  private String strConnectionIP;
  private int intConnectionPort;
  private String strPassword;
  private Session sesConnection;
  private int intTimeOut;

  private void doCommonConstructorActions(String userName, 
       String password, String connectionIP, String knownHostsFileName)
  {
     jschSSHChannel = new JSch();

     try
     {
        jschSSHChannel.setKnownHosts(knownHostsFileName);
     }
     catch(JSchException jschX)
     {
        logError(jschX.getMessage());
     }

     strUserName = userName;
     strPassword = password;
     strConnectionIP = connectionIP;
  }

  public SSHManager(String userName, String password, 
     String connectionIP, String knownHostsFileName)
  {
     doCommonConstructorActions(userName, password, 
                connectionIP, knownHostsFileName);
     intConnectionPort = 22;
     intTimeOut = 60000;
  }

  public SSHManager(String userName, String password, String connectionIP, 
     String knownHostsFileName, int connectionPort)
  {
     doCommonConstructorActions(userName, password, connectionIP, 
        knownHostsFileName);
     intConnectionPort = connectionPort;
     intTimeOut = 60000;
  }

  public SSHManager(String userName, String password, String connectionIP, 
      String knownHostsFileName, int connectionPort, int timeOutMilliseconds)
  {
     doCommonConstructorActions(userName, password, connectionIP, 
         knownHostsFileName);
     intConnectionPort = connectionPort;
     intTimeOut = timeOutMilliseconds;
  }

  public String connect()
  {
     String errorMessage = null;

     try
     {
        sesConnection = jschSSHChannel.getSession(strUserName, 
            strConnectionIP, intConnectionPort);
        sesConnection.setPassword(strPassword);
        // UNCOMMENT THIS FOR TESTING PURPOSES, BUT DO NOT USE IN PRODUCTION
        // sesConnection.setConfig("StrictHostKeyChecking", "no");
        sesConnection.connect(intTimeOut);
     }
     catch(JSchException jschX)
     {
        errorMessage = jschX.getMessage();
     }

     return errorMessage;
  }

  private String logError(String errorMessage)
  {
     if(errorMessage != null)
     {
        LOGGER.log(Level.SEVERE, "{0}:{1} - {2}", 
            new Object[]{strConnectionIP, intConnectionPort, errorMessage});
     }

     return errorMessage;
  }

  private String logWarning(String warnMessage)
  {
     if(warnMessage != null)
     {
        LOGGER.log(Level.WARNING, "{0}:{1} - {2}", 
           new Object[]{strConnectionIP, intConnectionPort, warnMessage});
     }

     return warnMessage;
  }

  public String sendCommand(String command)
  {
     StringBuilder outputBuffer = new StringBuilder();

     try
     {
        Channel channel = sesConnection.openChannel("exec");
        ((ChannelExec)channel).setCommand(command);
        InputStream commandOutput = channel.getInputStream();
        channel.connect();
        int readByte = commandOutput.read();

        while(readByte != 0xffffffff)
        {
           outputBuffer.append((char)readByte);
           readByte = commandOutput.read();
        }

        channel.disconnect();
     }
     catch(IOException ioX)
     {
        logWarning(ioX.getMessage());
        return null;
     }
     catch(JSchException jschX)
     {
        logWarning(jschX.getMessage());
        return null;
     }

     return outputBuffer.toString();
  }

  public void close()
  {
     sesConnection.disconnect();
  }

  }

For testing.

  /**
     * Test of sendCommand method, of class SSHManager.
     */
  @Test
  public void testSendCommand()
  {
     System.out.println("sendCommand");

     /**
      * YOU MUST CHANGE THE FOLLOWING
      * FILE_NAME: A FILE IN THE DIRECTORY
      * USER: LOGIN USER NAME
      * PASSWORD: PASSWORD FOR THAT USER
      * HOST: IP ADDRESS OF THE SSH SERVER
     **/
     String command = "ls FILE_NAME";
     String userName = "USER";
     String password = "PASSWORD";
     String connectionIP = "HOST";
     SSHManager instance = new SSHManager(userName, password, connectionIP, "");
     String errorMessage = instance.connect();

     if(errorMessage != null)
     {
        System.out.println(errorMessage);
        fail();
     }

     String expResult = "FILE_NAME\n";
     // call sendCommand for each command and the output 
     //(without prompts) is returned
     String result = instance.sendCommand(command);
     // close only after all commands are sent
     instance.close();
     assertEquals(expResult, result);
  }
2 of 9
36

This is a shameless plug, but I'm just now writing some extensive Javadoc for JSch.

Also, there is now a Manual in the JSch Wiki (written mainly by me).


About the original question, there is not really an example for handling the streams. Reading/writing a stream is done as always.

But there simply can't be a sure way to know when one command in a shell has finished just from reading the shell's output (this is independent of the SSH protocol).

If the shell is interactive, i.e. it has a terminal attached, it will usually print a prompt, which you could try to recognize. But at least theoretically this prompt string could also occur in normal output from a command. If you want to be sure, open individual exec channels for each command instead of using a shell channel. The shell channel is mainly used for interactive use by a human user, I think.

🌐
GitHub
github.com › mwiede › jsch › blob › master › ChangeLog
jsch/ChangeLog at master · mwiede/jsch
fork of the popular jsch library. Contribute to mwiede/jsch development by creating an account on GitHub.
Author   mwiede
🌐
GitHub
gist.github.com › LCamel › 5cb6a27dc4bd56080141227f088cb9ed
sftp / jsch · GitHub
sftp / jsch. GitHub Gist: instantly share code, notes, and snippets.