Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

com.jcraft.jsch.JSchException

Neeraj T
2009-04-01
2013-05-28
  • Neeraj T
    Neeraj T
    2009-04-01

    Hi,

    when i run the programme using Jsch APIs to list the files on the FileZill FTP server for a user “admin”
    i am getting following error

    *****************error start************
    "com.jcraft.jsch.JSchException

    : connection is closed by foreign host
    at com.jcraft.jsch.Session.connect(

    Session.java:271)
    at com.jcraft.jsch.Session.connect(

    Session.java:153)
    "
    *****************error end************

    please help me to solve the same.

    *********************program starts*************
    package test;
    import com.jcraft.jsch.*;
    import java.util.*;

    public class CLISftp{
      public static void main(String[] arg){

    Session session = null;
        try{
          JSch jsch=new JSch();

          String host="localhost";
          String user="admin";
          session=jsch.getSession(user, host, 21);
          session.setUserInfo(new MyUserInfo());
          session.setPassword("");
          session.connect();

          Channel channel=session.openChannel("sftp");
          channel.connect();
          ChannelSftp c=(ChannelSftp)channel;

          //get a directory listing
          Vector v = c.ls("*");
          for(int i = 0; i < v.size();i++){
           System.out.println(v.get(i));
          }
        }
        catch(Exception ex){
         ex.printStackTrace();
        }
        finally{
         //always disconnect in a finally block or the conenction will be hang
         session.disconnect();
        }
      }
     
      private static class MyUserInfo implements UserInfo {

    public String getPassphrase() {
      return null;
    }

    public String getPassword() {
      return null;
    }

    public boolean promptPassphrase(String arg0) {
      // TODO Auto-generated method stub
      return false;
    }

    public boolean promptPassword(String arg0) {
      // TODO Auto-generated method stub
      return false;
    }

    //There is no keystore on the clientside so just
    //accept the serverkey.
    public boolean promptYesNo(String arg0) {
      // TODO Auto-generated method stub
      return true;
    }

    public void showMessage(String arg0) {
      //System.out.println(arg0);
    }
      
      };
    }
    *******************end***************************