Seem to have found my issue. The following code works:

public class SFTPConnection {
private static JSch jsch = new JSch();
 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 {
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();
}
}

On 27 February 2012 23:25, Phil Hewitt <[email protected]> wrote:

> 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
>



-- 
Phil Hewitt

Tel: +44 (0) 1642 671111
Mob: +44 (0) 7703 483614
Skype: mrphewitt
Email: [email protected]
Web: www.enappsys.com
Address: EnAppSys Ltd, Arnison House, 139B The High Street, Yarm,
Stockton-on-Tees TS15 9AY U.K.

This communication contains information that is confidential and may also
be legally privileged. It is for the exclusive use of the intended
recipient(s). If you are not the intended recipient(s) please do not read
it and note that any distribution, copying or use of this communication or
the information therein is strictly prohibited.If you have received this
communication in error, please notify the sender immediately and then
destroy all copies of it. This company accepts no liability whatsoever for
the views or opinions of the author in any matter not related to the
company's object. EnAppSys Ltd reserves the right to monitor all e-mail
messages passing through our network.

Registered Office: EnAppSys Ltd, Arnison House, 139B The High Street, Yarm,
Stockton-on-Tees TS15 9AY U.K.
Registered in England and Wales No. 4685938
------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
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-d2d
_______________________________________________
JSch-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jsch-users

Reply via email to