I am trying to data from a server. Sometimes my code fails due to an UnknownHostException
. Why is that? What is the cause of this problem?
-
1An UnknownHostException is thrown if a java.net.UnknownHostException occurs while creating a connection to the remote host for a remote method call. Something isn't resolving at the DNS level this isn't a java or an xml problem. – May 25 '10 at 17:07
5 Answers
This may occur if a hiccup in DNS server has occurred. Apart from making the DNS server more robust or looking for another one, you can also just use the full IP address instead of the hostname. This way it doesn't need to lookup the IP address based on the hostname. However, I would rather fix the DNS issue and prefer the DNS since IP addresses may change from time to time.
-
Thanks BalusC :). Great, you gave the ip. Now the DNS itself doesn't come into picture. But it's not working. Now i get this error :- java.io.FileNotFoundException: http://216.115.98.240/rss/india – TCM May 25 '10 at 17:07
-
Even when i type "http://216.115.98.240/rss/india" i get nothing. Is this ip correct? When i did ping i got this ip 216.115.97.236. However in this also i get the same exception in java and in browser also nothing displays. – TCM May 25 '10 at 17:21
An UnknownHostException
indicates the host specified couldn't be translated to an IP address. It could very well be a problem with your DNS server.

- 80,126
- 17
- 159
- 190
-
Ok MarkPeters, so how should i resolve this? since i have to submit my work in college and i don't think i will able to have access to college's router and change the dns server in it :( – TCM May 25 '10 at 17:22
-
1@Nitesh: Is it failing in the college's environment or only in your local environment? If it's inconsistently failing from the college's environment, I would both notify your teacher or tutor that there may be an environment problem that's out of your control, and also try to lobby your college IT staff to diagnose and fix the problem. You might consider an approach where you catch the exception and try again a few times, perhaps a few seconds/minutes apart. – Mark Peters May 25 '10 at 18:01
-
//Override system DNS setting with Google free DNS server System.setProperty("sun.net.spi.nameservice.nameservers", "8.8.8.8"); System.setProperty("sun.net.spi.nameservice.provider.1", "dns,sun"); – ACV Sep 04 '15 at 21:24
If the DNS resolution fails intermittently, catch the exception and try again until you get name resolution. You can only control, what you can control... And if you can't control/fix the DNS server, make your app robust enough to handle the quirky DNS server.

- 11
- 1
I too am seeing sporadic UnknownHostExceptions in Java for no apparent reason. The solution is just to retry a few times. Here is a wrapper for DocumentBuilder.parse that does this:
static Document DocumentBuilder_parse(DocumentBuilder b, String uri) throws SAXException, IOException {
UnknownHostException lastException = null;
for (int tries = 0; tries < 2; tries++) {
try {
return b.parse(uri);
} catch (UnknownHostException e) {
lastException = e;
System.out.println("Retrying because of: " + e);
continue;
}
}
throw lastException;
}

- 1,000
- 9
- 12
i used the example of this post and in my case was the java version. With jdk1.6 fails, but with a newer one it didn't have any problem.
https://forums.freebsd.org/threads/java-problem-inetaddress-getlocalhost-is-not-working.26618/

- 31
- 4