Java Getlocalhost() Unknownhostexception /Etc/Hosts File Differs Linux API

java getLocalHost() UnknownHostException /etc/hosts file differs linux api?

ah, I stumbled upon the difference. In my /etc/sysconfig/network file, I had the FQDN on the working machine but on the other machine, I only had the hostname "b" so changing that to "b.domain.com" fixed the issue and I don't need to modify my /etc/hosts file at all!!!!

Dean

Java - UnknownHostException

you might not have mapping of localhost in your system32\drivers\etc\hosts (win) /etc/hosts (linux)

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.

ServerName vs Hostname ( /etc/sysconfig/network vs /etc/hosts ) what is the difference?

The hostname is more of a useful identifier of your server. ( https://serverfault.com/questions/228102/hostnames-what-are-they-all-about )

You do not need to register the domain name you use as a hostname unless you intend the domain to be accessible to the world.

The reason you have to edit hosts is because, as you pointed out, the domain is not registered and there is no DNS entry for it. The hosts file acts like a local DNS cache. So you OS looks to the hosts file first while trying to resolve the domain. ( http://en.wikipedia.org/wiki/Hosts_(file) )

The virtualhost entry in your httpd.conf file is for apache to recognize the "host" that a request was destined for and route it to the appropriate file structure on disk.

If you setup a VirtualHost entry for a domain that was un-resolvable it would not work and you would get a domain name cannot be resolved. (this is what the hosts file entry fixes for domains who are not registered and have DNS entries.)

I hope that helps :)

Can InetAddress represent host names that can't be resolved?

I guess that this is not possible, because the core function of InetAddress is to handle IP-Addresses:

This class represents an Internet
Protocol (IP) address.

An IP address is either a 32-bit or
128-bit unsigned number used by IP, a
lower-level protocol on which
protocols like UDP and TCP are built.
The IP address architecture is defined
by RFC 790: Assigned Numbers, RFC
1918: Address Allocation for Private
Internets, RFC 2365: Administratively
Scoped IP Multicast, and RFC 2373: IP
Version 6 Addressing Architecture. An
instance of an InetAddress consists of
an IP address and possibly its
corresponding host name (depending on
whether it is constructed with a host
name or whether it has already done
reverse host name resolution).

You have no possibility to construct an InetAddress object without an IP-address.

For you purpose you need to define a Hostname.class, which holds the hostname and maybe additional data. This class should/can handle all the domainname internationalization via IDN, if you are using Java 6. There are separate IDN libraries available if you are on Java 5.

A separate Hostname.class can also define guards or constructors, which assure only valid hostnames according to the relevant RFCs.



Related Topics



Leave a reply



Submit