?
Solved

java.net.InetAddress.getLocalHost() problem

Posted on 2003-02-27
13
Medium Priority
?
1,609 Views
Last Modified: 2010-05-18
In class java.net.InetAddress

static InetAddress getLocalHost()
     throws UnknownHostException

returns the local host data (i.e. the ip number of some default interface of the machine).

I have one machine where it throws the UnknownHostException. I wonder under which circumstances it should do that? There is no security manager involved so this cant be the reason.

The machine runs a linux kernel 2.4.7 and the jdk version is 1.2.2. The "hostname" command works and the machine HAS a eth0 interface with an ip number. Other applications (not java based) work fine, too, for example apache httpd and sendmail.

The same program on another machine would not throw the exception and give me the correct ip number.

Any help is appreciated. Thanks.

Moritz
0
Comment
Question by:kruemelmo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
13 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8032612
> The same program on another machine would not throw the exception and give me the correct ip number.

  Have you checked the "/etc/hosts" file and see if everything is fine there?
0
 

Author Comment

by:kruemelmo
ID: 8032635
/etc/hosts is fine. The ip number of eth0 is mapped to the host name there.

However, I would believe that getLocalHost() should be able to find the ip number of some network interface without a host entry... The class is "InetAddress", not "HostName"...

Moritz
0
 
LVL 35

Expert Comment

by:girionis
ID: 8032733
 It should true but I just wanted to make sure, just in case. You also know that the InetAddress has a cache. Taken from the javadoc:

"The InetAddress class has a cache to store successful as well as unsuccessful host name resolutions. The positive caching is there to guard against DNS spoofing attacks; while the negative caching is used to improve performance.

By default, the result of positive host name resolutions are cached forever, because there is no general rule to decide when it is safe to remove cache entries. The result of unsuccessful host name resolution is cached for a very short period of time (10 seconds) to improve performance."

  Can you try using the:

networkaddress.cache.negative.ttl

  property with the value set to 0 (never cache):

java -Dnetworkaddress.cache.negative.ttl=0 <your application>
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 86

Expert Comment

by:CEHJ
ID: 8032786
Listening...
0
 
LVL 6

Expert Comment

by:mightyone
ID: 8037173
.. me too.
0
 

Author Comment

by:kruemelmo
ID: 8040821
girionis,

i have found that javadoc api documentation, too, but in jdk 1.4. We use 1.3.1 here and that one seems not to have a cache. (I had said we used 1.2, this is wrong, it is 1.3.1 indeed.)

Unfortunately it's a production machine and i cant try "random" things there... any hint that the cache property settings would solve the issue even with jdk 1.3.1?

Moritz
0
 

Author Comment

by:kruemelmo
ID: 8041517
girionis,

i have found that javadoc api documentation, too, but in jdk 1.4. We use 1.3.1 here and that one seems not to have a cache. (I had said we used 1.2, this is wrong, it is 1.3.1 indeed.)

Unfortunately it's a production machine and i cant try "random" things there... any hint that the cache property settings would solve the issue even with jdk 1.3.1?

Moritz
0
 
LVL 1

Accepted Solution

by:
tenderfoot earned 400 total points
ID: 8043464
Just a couple of suggestions, since it’s quite simple to reproduce the problem on the prod machine, it would be helpful if you could knock up a small app to print both the error message from UnknownHostException and the stack trace. This might provide us with more information to go on.

Also may be worth obtaining a copy of the local community source (Java API src + native), this way you can almost certainly verify if any/how the form of caching has been implemented on your version of the jre.

With the stack trace, you should be able to nail exactly where the problem is.

InetAddress caching has changed quite significantly across different versions of jre, I believe policy is still maintained by sun.net.AddressCachePolicy.class . The version I have, 1.3.1_01 on a SunOs has caching enabled but utilises a different property value for managing  timeout on hostname lookups.. (hence the need for the API source)

java -Dnetworkaddress.cache.negative.ttl=0 LocalHostFailure
Default policy [-1]
FOREVER [-1]
NEVER [0]

java -Dsun.net.inetaddr.ttl=10 LocalHostFailure
Default policy [10]
FOREVER [-1]
NEVER [0]


import sun.net.InetAddressCachePolicy ;

public class LocalHostFailure
{

  public static void main(String args[])
  {

    System.out.println("Default policy [" + InetAddressCachePolicy.get()+ "]") ;

    System.out.println("FOREVER [" + InetAddressCachePolicy.FOREVER + "]") ;
    System.out.println("NEVER [" + InetAddressCachePolicy.NEVER + "]") ;
  }
}

Link below may offer some help

http://developer.java.sun.com/developer/bugParade/bugs/4065037.html 

0
 

Author Comment

by:kruemelmo
ID: 8043839
tenderfoot, thanks for your comments. I should have posted a stack trace anyway. Here it is.

Test program:
---------------------------------------
import java.net.InetAddress;
import java.lang.*;

class inettest
{
  public static void main(String argv[])
                throws java.net.UnknownHostException
  {
    InetAddress i = InetAddress.getLocalHost();
    System.out.println(i.toString());
  }
}

Exception in thread "main" java.net.UnknownHostException: dokuweb02.xxx.yyy [tld deleted]
        at java.net.InetAddress.getAllByName0(InetAddress.java:566)
        at java.net.InetAddress.getAllByName0(InetAddress.java:535)
        at java.net.InetAddress.getAllByName(InetAddress.java:528)
        at java.net.InetAddress.getLocalHost(InetAddress.java:718)
        at inettest.main(inettest.java:10)

The DNS is not able to find the given host name and that's most certainly the difference between the two machines I tested. Looking at the decompiled implementation of getLocalHost(), the static initializer of InetAddress fetches the local host data from the private class InetAddressImpl which has a native method "getLocalHostName()". getLocalHost() seems to try to resolve the name returned by that. This fails on the problem machine; there is a /etc/hosts entry, but not one including the domain name and this is the problem.

I understad that this strategy to find a ip number for the local host is fine... however i still am not able to get *any* info about the local host if DNS is somehow broken.

Now I wonder why there is no public method to get the local host name... just in case there is no way to get a local interface ip address. To say the truth, what we need is a function to *somehow* identify the machine.

Since this question is different from the original one, I better open a new one.

Moritz
0
 
LVL 35

Expert Comment

by:girionis
ID: 8044528
> This fails on the problem machine; there is a /etc/hosts entry, but not one including the domain name and this is the problem.

  I thought you told us everything is fine on the /etc/hosts file.

  Anyway, can you not just add the domain name there? The hosts file is very important and this is how Unix/Linux operating systems manage hosts and load information about them, usually at start-up time.

  Besides the call to getLocalHost is platform and network dependent. *nix systems refer to the hosts file when they try to resolve hostnames to IP addresses *before* they use any name servers. If the IP address is in the hosts file then no name servers are used. If your system contains IP addresses that are not listed under the DNS then it is recommended to add these IP addresses in the hosts file.

> however i still am not able to get *any* info about the local host if DNS is somehow broken.

  Look at my answer above and you realize that this hasn't got anyhting to do with Java. The only way to get information (on *nix systems) about any host if the host is not listed in a DNS is to add the host in the hosts file.
0
 

Author Comment

by:kruemelmo
ID: 8055421
girionis,

thanks for your comments and I agree and understand that broken or unavailable DNS will not harm if the /etc/hosts entries are correct. Also in the beginning I hadn't realised that there is no correct /etc/hosts entry (which includes the domain name).

The comment from tenderfoot helped me to understand how the getLocalHost() function call works, and that is to obtain the local host *name* from the operating system and then resolve it to get the ip address. I was unaware of the fact that in order to get the ip address of *any* interface on a Unix/Linux host, you have to start at the (gethostname()) system name.

>> however i still am not able to get *any* info about the local host if DNS is somehow broken.

> Look at my answer above and you realize that this hasn't got anyhting to do with Java.

Here I disagree. There is something called the "host name" which is independent from the interfaces and can be obtained in C by calling gethostname().

I will ask the customer to add the domain name in /etc/hosts. However it would be safer to gethostname() somehow, in case ithe installation is broken.

Moritz
0
 
LVL 35

Expert Comment

by:girionis
ID: 8056085
> Here I disagree. There is something called the "host name" which is independent from the interfaces and can be obtained in C by calling gethostname().

  So it is obtained by some means of JNI or some other native API calls which depend on the operating system. Therefore afaik the calling of the "gethostname" is platform dependent, otherwise -if it were java dependent- you would have had problems with every computer your app was running on.

  Tbh it's a very common problem. It has happened a lot in the past, a java app that works fine under Windows throws exceptions under Unix/Linux and all this because these os obtain network specific information in an os dependent manner.

  Anyhow it's solved now and next time it happens we have more info and experience of how to deal with it :-)
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month15 days, 2 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question