Search code examples
javasftpsshdapache-mina

Apache Mina SSHD 1.0 Server exits immediately


I am using Apache Mina sshd-core-1.0.0 to start an SFTP daemon. The program however exits after the sshd.start(). There are no errors. However if I use sshd-core-0.0.14, the server starts just fine and I can initiate an SFTP session. Am I missing something with 1.0.0?

Code snippet with 1.0.0 (does not work)

public static void main(String[] args) throws IOException {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(2222);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File ("hostkey.ser")))   
    sshd.setPasswordAuthenticator(new AuthenticatorImpl());
    sshd.start();
}

Code snippet with 0.0.14 (works)

public static void main(String[] args) throws IOException {
    SshServer sshd = SshServer.setUpDefaultServer();
    sshd.setPort(2222);
    sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider("hostkey.ser")); 
    sshd.setPasswordAuthenticator(new AuthenticatorImpl());
    sshd.start();
}

The following is output when 1.0.0 runs. The code starts fine but terminates after the sshd.start() statement.

2015-12-16 19:57:38,510 DEBUG SFTPServer.main(SFTPServer.java:26) message
2015-12-16 19:57:38,767 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:145) Trying to register BouncyCastle as a JCE provider
2015-12-16 19:57:39,076 INFO org.apache.sshd.common.util.SecurityUtils$BouncyCastleRegistration.call(SecurityUtils.java:149) Registration succeeded
2015-12-16 19:57:39,105 DEBUG org.apache.sshd.common.io.nio2.Nio2Acceptor.bind(Nio2Acceptor.java:57) Binding Nio2Acceptor to address 0.0.0.0/0.0.0.0:2222
2015-12-16 19:57:39,114 INFO SFTPServer.main(SFTPServer.java:32) Started

Solution

  • The SshServer.Start only starts listening on the incoming port. It does not block. So the main is terminated immediately afterwards. This should not be any different in 0.0.14, though I cannot try.

    You have to wait in the main explicitly to keep the server running.

    See how SshServer.main is implemented (both in 0.0.14 and 1.0.0):

    public static void main(String[] args) throws Exception {
    
        ...
    
        SshServer sshd = SshServer.setUpDefaultServer();
    
        ...
    
        sshd.start();
    
        Thread.sleep(Long.MAX_VALUE);
    }