java.net.UnknownHostException: Invalid hostname for server: local
Asked Answered
D

14

149

What are the steps I should take to solve the error:

java.net.UnknownHostException: Invalid hostname for server: local

I added the new virtual host name at Android emulator but the result returns to

 java.net.UnknownHostException virtualhostname at 
  java.net.InetAddress.lookUpHostByName(InetAddress.java:506)

When I type my virtualhost URL on my PC, it works on display. Then again, when I ran on Emulator and check on Logcat, I couldn't be able to read or check the HTTP status if 200, 202, or an error code number. It simply returned to UnknownHostException

Danby answered 26/6, 2011 at 13:34 Comment(3)
a bit more information would help us to solve the problem easierButanone
are you behind a Proxy server and configured the proxy settings? If so, you might need to exclude localhost and other hosts which don't need the Proxy server's help to resolve to a valid IP Address.Dudgeon
I added the new virtual host name at Android emulator but the result returns to "java.net.UnknownHostException virtualhostname at java.net.InetAddress.lookUpHostByName(InetAddress.java:506)". When I type my virtualhost URL on my PC, it works on display. Then again, when I ran on Emulator and check on Logcat, I couldn't be able to read or check the HTTP status if 200, 202, or an error code number. It simply returned to "UnknownHostException"Ayeaye
A
72

What the exception is really saying is that there is no known server with the name "local". My guess is that you're trying to connect to your local computer. Try with the hostname "localhost" instead, or perhaps 127.0.0.1 or ::1 (the last one is IPv6).

From the javadocs:

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

127.0.0.1or ::1 or "localhost" should always be the loopback interface, so if that doesn't work I'd be really surprised.

If there really is a server called "local" on your network - examine your DNS settings or add it to your hosts file.

Allograph answered 26/6, 2011 at 13:37 Comment(1)
On some implementations, a restart of the JVM might be necessary for gateway/dns changes to take effect, apparently.Berwickupontweed
H
85

I was having the same issue on my mac. I found the issue when I pinged my $HOSTNAME from terminal and it returned ping: cannot resolve myHostName: Unknown host.

To resolve:

  1. Do echo $HOSTNAME on your terminal.

  2. Whatever hostname it shows (lets say myHostName), try to ping it : ping myHostName. If it returns ping: cannot resolve myHostName: Unknown host then add an entry into your /etc/hosts file.

  3. For that edit /etc/hosts file and add following:

    127.0.0.1 myHostName

Hellman answered 1/11, 2014 at 4:42 Comment(2)
Wonder how it works on some machines out of the box and on some we have to add the hosts entryPurposely
@Skynet depends on the linux distribution I guessSpecious
A
72

What the exception is really saying is that there is no known server with the name "local". My guess is that you're trying to connect to your local computer. Try with the hostname "localhost" instead, or perhaps 127.0.0.1 or ::1 (the last one is IPv6).

From the javadocs:

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

127.0.0.1or ::1 or "localhost" should always be the loopback interface, so if that doesn't work I'd be really surprised.

If there really is a server called "local" on your network - examine your DNS settings or add it to your hosts file.

Allograph answered 26/6, 2011 at 13:37 Comment(1)
On some implementations, a restart of the JVM might be necessary for gateway/dns changes to take effect, apparently.Berwickupontweed
U
16

java.net.UnknownHostException: Host is unresolved:

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

This exception is also raised when you are connected to a valid wifi but router does not receive the internet. Its very easy to reproduce this:

  1. Connect to a valid wifi
  2. Now remove the cable from the router while router is pluged-in

You will observe this error!!

You can't really solve this, You can only notify the user gracefully. (something like - "Unable to make a connection")

Unmannered answered 19/9, 2012 at 21:32 Comment(5)
It would be more general to say that the exception is raised when the host isn't found in DNS or when the DNS itself can't be found, e.g. for any network connectivity reason, or a misconfiguration.Quadri
So how do you fix this problem?Ingaborg
You can't really solve this, You can only notify the user gracefully. (something like - "Unable to make a connection")Unmannered
What does it mean to have this problem in a school network but not my personal connection at home?Scorch
@Scorch May be your school network requires some id/pwd authentication first.. Try opening any web url in browser and see if that is properly displayedUnmannered
F
11

This is not specific to the question, but this question showed up when I was Googling for the mentioned UnknownHostException, and the fix is not found anywhere else so I thought I'd add an answer here.

The exception that was continuously received was:

java.net.UnknownHostException:  google.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 java.net.Socket.connect(Socket.java:538)
    at java.net.Socket.<init>(Socket.java:434)
    at java.net.Socket.<init>(Socket.java:211)
    ... 

No matter how I tried to connect to any valid host, printing it in the terminal would not help either. Everything was right.

The Solution

Not calling trim() for the host string which contained whitespace. In writing a proxy server the host was obtained from HTTP headers with the use of split(":") by semicolons for the HOST header. This left whitespace, and causes the UnknownHostException as a host with whitespace is not a valid host. Doing a host = host.trim() on the String host solved the ambiguous issue.

Forland answered 29/11, 2015 at 7:51 Comment(2)
Thank you - this saved me. In my case the String host being passed to the function had a \n suffix, so running .trim() on that fixed it for me!Tomboy
Thanks, I spent too much trying to figure out what the problem was until I realized I typed a blank space between two different IP addresses.Probably
F
7

Your hostname is missing. JBoss uses this environment variable ($HOSTNAME) when it connects to the server.

[root@xyz ~]# echo $HOSTNAME
xyz

[root@xyz ~]# ping $HOSTNAME
ping: unknown host xyz

[root@xyz ~]# hostname -f
hostname: Unknown host

There are dozens of things that can cause this. Please comment if you discover a new reason.

For a hack until you can permanently resolve this issue on your server, you can add a line to the end of your /etc/hosts file:

127.0.0.1 xyz.xxx.xxx.edu xyz
Frothy answered 4/12, 2015 at 18:11 Comment(1)
Preventative maintenance: unix.stackexchange.com/questions/248616/…Frothy
M
5

This might happen due to various reasons

1) Check if you have VPN connected, you might get this error sometimes if yes

"Your hostname, localhost resolves to a loopback address: 127.0.0.1; using 10.xxx.1.193 instead (on interface cscotun0)"

2) Check your $HOSTNAME

3) try to ping $HOSTNAME on commandline and if it doesnt work, tweak the system settings to make your local host respond to pings

Madaras answered 28/5, 2014 at 14:45 Comment(0)
M
4

Try the following :

String url = "http://www.google.com/search?q=java";
URL urlObj = (URL)new URL(url.trim());
HttpURLConnection httpConn = 
(HttpURLConnection)urlObj.openConnection();
httpConn.setRequestMethod("GET");
Integer rescode = httpConn.getResponseCode();
System.out.println(rescode);

Trim() the URL

Marika answered 7/2, 2018 at 10:36 Comment(1)
Trimming a hard-coded string with no spaces makes no sense.Grimes
O
2

Please try to set SPARK_LOCAL_IP environment variable to the ip address(can be localhost i.e. your own ip address) you want to connect. E.g.

$ export SPARK_LOCAL_IP=182.95.208.152

This way you will not be required to alter existing linux settings. Worked for me, hope helps you too.

Orgell answered 21/9, 2016 at 5:6 Comment(0)
B
1

Trying to connect to your local computer.try with the hostname "localhost" instead or perhaps ::/ - the last one is ipv6

Burka answered 22/5, 2013 at 6:47 Comment(0)
S
0

Connect your mobile with different wifi connection with different service provider. I don't know the exact issue but i could not connect to server with a specific service provider but it work when i connected to other service provider. So try it!

Semen answered 20/8, 2018 at 6:44 Comment(0)
C
0

I had this issue in my android app when grabbing an xml file the format of my link was not valid, I reformatted with the full url and it worked.

Canonical answered 15/9, 2019 at 19:37 Comment(0)
S
0

If you are here because your emulator gives you that Exception, Go to Tools > AVD Manager in your android emulator and Cold boot your Emulator.

Silda answered 3/11, 2019 at 17:42 Comment(0)
F
0

I had the same issue.

Restart docker was the fix for me. For some reason it needed a restart, I don´t know why, but it worked.

Flash answered 20/6, 2022 at 9:9 Comment(0)
N
-1

If your /etc/localhosts file has entry as below: Add hostname entry as below:

127.0.0.1 local host (add your hostname here)
::1 (add hostname here) (the last one is IPv6).

This should resolve the issue.

Nullipore answered 18/7, 2017 at 17:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.