I am receiving an error when I connect to an SFTP server that I have
previously connected to just 10 minutes before.
Please find below the code fragments I think I have made a mistake with.
public class SFTPConnection {
private Session session = null;
private Channel channel = null;
private ChannelSftp sftpChannel = null;
public void connect(final int port, final String host, final String
username, final String password, final String basePath)
throws JSchException, SftpException {
final JSch jsch = new JSch();
this.session = jsch.getSession(username, host, port);
this.session.setPassword(password);
this.session.setConfig("StrictHostKeyChecking", "no");
this.session.connect();
this.channel = this.session.openChannel("sftp");
this.channel.connect();
this.sftpChannel = (ChannelSftp) this.channel;
this.sftpChannel.cd(basePath);
}
public void exit() {
this.sftpChannel.exit();
this.channel.disconnect();
this.session.disconnect();
}
}
These fragments are called by another class that uses the connect() method
to connect to the server, changes directory using chdir, reads the
directory using ls, copies files out of the directory using get, removes
the files from the directory using rm and then uses the exit() method above
to close the connections.
Ten minutes later it re-runs the code and then it throws an Exception. I
assume because I have not correctly closed the connection properly
previously. See below for the stack dump:
Caused by: com.jcraft.jsch.JSchException: java.lang.NullPointerException
at com.jcraft.jsch.Session.connect(Session.java:175)
at com.jcraft.jsch.Session.connect(Session.java:160)
at
com.enappsys.wrappers.sftp.SFTPConnection.connect(SFTPConnection.java:51)
at
com.enappsys.wrappers.sftp.SFTPConnectionFactory.createSFTPConnection(SFTPConnectionFactory.java:49)
... 3 more
Caused by: java.lang.NullPointerException
at com.jcraft.jsch.JSch.getConfig(JSch.java:291)
at com.jcraft.jsch.Session.getConfig(Session.java:1881)
at com.jcraft.jsch.Session.connect(Session.java:171)
... 6 more
Any help and advice would be appreciated
Regards,
Phil Hewitt
------------------------------------------------------------------------------
Try before you buy = See our experts in action!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-dev2
_______________________________________________
JSch-users mailing list
JSch-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jsch-users