Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UnknownHostException occures when connecting SFTP using Java

I am Trying to upload files continuously to SFTP server using 5 threads in java, at starting program uploads files correctly but after some time ,

All threads throws UnknownHostException when trying to create new session and Exception continues upto 5 to 10 minutes,after some time program works normally, i cant able to find what will cause for this Exception,

This is the code used for connecting sftp,

        JSch jsch = new JSch();
        jsch.setKnownHosts(host_file);
        session = jsch.getSession(SFTPUSER,SFTPHOST,SFTPPORT);
        session.setPassword(SFTPPASS);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        channel = session.openChannel("sftp");
        channel.connect();

Exception :

 at td.bdops.clupload.CARUpload.uploadZip(CARUpload.java:398)
    at td.bdops.clupload.CARUpload.uploadZip(CARUpload.java:398)
Caused by: java.net.UnknownHostException: sftp.opsbank2-prod.tio.systems
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at com.jcraft.jsch.Util.createSocket(Util.java:343)
    at com.jcraft.jsch.Session.connect(Session.java:215)
    at com.jcraft.jsch.Session.connect(Session.java:183)
    at td.bdops.util.FTPUtility.uploadAWSFTP(FTPUtility.java:227)
    at td.bdops.util.FTPUtility.uploadAWSFTP(FTPUtility.java:247)

can anyone please explain me, what is the root cause this error

like image 358
radhakrishnan Avatar asked Oct 29 '22 02:10

radhakrishnan


1 Answers

All threads throws UnknownHostException when trying to create new session and Exception continues up to 5 to 10 minutes,after some time program works normally, i cant able to find what will cause for this Exception...

This is pretty self explanatory. Reading the javadocs for UnknownHostException:

Thrown to indicate that the IP address of a host could not be determined.

Looking at the code for AbstractPlainSocketImpl I see:

if (addr.isUnresolved())
   throw new UnknownHostException(addr.getHostName());

So your sftp.opsbank2-prod.tio.systems hostname does not resolve. That means that Java's name resolving code can't determine what the IP for that hostname is.

Here are some things to try:

  • Use the IP of that hostname instead of the name.
  • Use dig or host commands to lookup that hostname on that system to see if it resolves.
  • Try the following line of code to see if it works. It too should throw:

    new java.net.Socket("unknown.host.should.throw.com", 80).close();
    
  • Try your hostname now:

    new java.net.Socket("sftp.opsbank2-prod.tio.systems", 80).close();
    

If you see that your hostname is not resolving then you'll need to add it to the DNS configurations or /etc/hosts file. If you already have then there's something wrong with those files and you'll need to recheck your configuration.

like image 198
Gray Avatar answered Nov 15 '22 12:11

Gray