3

My program runs perfectly for sometime time, but after that I get an error

java.net.UnknownHostException: www.sears.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:666)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1534)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at extractData.ReviewsSearch.getJsonResponse(ReviewsSearch.java:28)
at main.java.DepartmentCategories_Main.main(DepartmentCategories_Main.java:110)

java.net.UnknownHostException: www.sears.com
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:432)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:527)
at sun.net.www.http.HttpClient.<init>(HttpClient.java:211)
at sun.net.www.http.HttpClient.New(HttpClient.java:308)
at sun.net.www.http.HttpClient.New(HttpClient.java:326)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1167)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1103)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:997)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:931)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1511)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439)
at extractData.productHierarchySearch.getHierarchy(productHierarchySearch.java:27)
at main.java.DepartmentCategories_Main.main(DepartmentCategories_Main.java:115)

Any idea, what can be the reason? Let me know if more information is required

edit: I tried ping command and I realized it is the internet connection. I am running the program in an ssh server and want to know how can I keep the code from stopping. I want it to wait for the connection.

3 Answers3

0

It seems your server can't figure out the IP address of the server. Can you check whether DNS is working properly when this error pops up? Just open a command prompt window and try to ping that server www.sears.com

Tharaka Devinda
  • 1,952
  • 21
  • 23
0

It emerges when you are trying to connect to a remote host using its host name, but the IP address of that host cannot be resolved.

Form Docs:

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

How to solve UnknownHostException?

UnknownHostException designates a pretty straight forward problem. That the IP address of the remote host you are trying to reach cannot be resolved. So the solution to this is very simple. You should check the input of Socket (or any other method that throws an UnknownHostException), and validate that it is the intended one. If you are not whether you have the correct host name, you can launch a UNIX terminal and use the nslookup command (among others) to see if your DNS server can resolve the host name to an IP address successfully. Here is an example :

nikos@nikos:~$ nslookup www.google.com
Server:     127.0.1.1
Address:    127.0.1.1#53

Non-authoritative answer:
Name:   www.google.com
Address: 173.194.39.209
Name:   www.google.com
Address: 173.194.39.210
Name:   www.google.com
Address: 173.194.39.212
Name:   www.google.com
Address: 173.194.39.211
Name:   www.google.com
Address: 173.194.39.208

nikos@nikos:~$ 

If you are on Windows you can use the host command. If that doesn’t work as expected then, you should check if the host name you have is correct and then try to refresh your DNS cache. If that doesn’t work either, try to use a different DNS server, eg Google Public DNS is a very good alternative.

Reference:

http://examples.javacodegeeks.com/core-java/net/unknownhostexception/java-net-unknownhostexception-how-to-solve-unknownhostexception/

0

Other answers address the particular exception you are seeing. However, you (probably) are really asking what to do about it.

As you have encountered, in the real world things sometimes fail (and throws exceptions). In those cases in which the exception is expected to be temporary, you should be catching the exception and arranging to retry the operation. You may want to retry immediately, but more often it is better to wait a bit before trying again. The idea is to let whatever problem is happening get fixed (or fix itself) before trying again.

It is often a good idea to log the exception (or at least a summary), in case operator intervention may be required to get things going again.

If you cannot proceed until that operation is successful, you probably want to retry in a loop. If you can work on other stuff, the retry might be implemented by putting the problem item at the back of the queue of work.

For the former case:

doWork("www.sears.com");

becomes

while (true) {
    try {
        doWork("www.sears.com");
        break;
    } catch (UnknownHostException e) {
        logger.log(e.getMessage());
        Thread.sleep(10000);
    }
}
Rob
  • 6,247
  • 2
  • 25
  • 33