Inetaddress.Getlocalhost() Throws Unknownhostexception

InetAddress.getLocalHost() throws UnknownHostException

In good tradition, I can answer my own question once again:

It seems that InetAddress.getLocalHost() ignores the /etc/resolv.conf, but only looks at the /etc/hosts file (where I hadn't specified anything besides localhost). Adding the IP and hostname to this file solves the problem and the exception is gone.


Another answer is almost correct and I got hint from above and my problem get resolved...Thanks.

But to improve this, I am adding steps-by-steps changes, so that it will be helpful for even naive users.

Steps:

  • Open /etc/hosts, the entries might look like below.

     127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4  
    ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  • You need to add one more line above of this by any editor like vi or gedit (e.g. <your-machine-ip> <your-machine-name> localhost).

     192.168.1.73 my_foo localhost

Now, overall file may look like this:

192.168.1.73 my_foo localhost
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  • Just save it and run again your Java code... your work is done.

Java: InetAddress.getLocalHost(); throws java.net.UnknownHostException

I figured out what the problem was: My hostname consisted of cyrillic letters. A user on Discord explained it to me like that:

this seems to be a limitated at the C function getaddrinfofunction
[12:14] which does make sense, considering cyrillic isn't a valid DNS
name, so it's following RFC. [12:14] (even though hostname != dns name
techically, but nuances). [12:15] RFC 952 and RFC 1123 have details
info on it

InetAddress.getLocalHost().getHostName() throws UnknownHostException

try this:

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
* @author Crunchify.com
*/

public class CrunchifyGetIPHostname {

public static void main(String[] args) {

InetAddress ip;
String hostname;
try {
ip = InetAddress.getLocalHost();
hostname = ip.getHostName();
System.out.println("Your current IP address : " + ip);
System.out.println("Your current Hostname : " + hostname);

} catch (UnknownHostException e) {

e.printStackTrace();
}
}
}

you test this code online on http://www.browxy.com/

i have taken it from http://crunchify.com/how-to-get-server-ip-address-and-hostname-in-java/

Why does this usage of InetAddress.getLocalHost() work okay with Java 6 but fails with Java 7 on OSX

I am guessing you will find the answer here - even though that question is about Solaris, both Mac OS X and Solaris are Unix operating systems.

InetAddress.getAllByName() throws UnknownHostException

The hostname shouldn't include a protocol. The host is the same, no matter what protocol you intend to use with it. Whether the later HTTPS connection fails or not, InetAddress.getAllByName() is unrelated to it (it doesn't and can't guarantee success or failure).

You're dealing with DNS only at this point, so it's just foo.com or 123.45.67.89 or an IPv6 address.



Related Topics



Leave a reply



Submit