Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8062840

InetAddress.getLocalhost() does not give same result in java7 and java8

XMLWordPrintable

    • x86
    • linux

      FULL PRODUCT VERSION :
      1.8.0_20-b26

      ADDITIONAL OS VERSION INFORMATION :
      2.6.32-431.11.2.el6.x86_64

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      /etc/hosts contains as first line:

      127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 <actual computer name as seen on local network>

      Java is run with option -Djava.net.preferIPv4Stack=true

      A DESCRIPTION OF THE PROBLEM :
      InetAddress.getLocalhost().getHostName() returns "localhost" when using java8, while it returns the actual computer name as seen on local network with java 7.

      If I change my /etc/hosts file to have the actual computer name as seen on local network as first alias (before localhost) for address 127.0.0.1 then the problem doesn't occur any more (e.g. I get the actual computer name, not localhost, on either java versions)

      REGRESSION. Last worked in version 7u51

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      run java with option -Djava.net.preferIPv4Stack=true

      use a /etc/hosts file that contains localhost as first alias for 127.0.0.1 and the actual computer name as last alias on the line.

      run a program that prints the result of InetAddress.getLocalhost().getHostName();

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      actual computer name as seen on network
      ACTUAL -
      localhost

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.io.BufferedReader;
      import java.io.IOException;
      import java.io.InputStreamReader;
      import java.net.InetAddress;
      import java.net.UnknownHostException;
      import java.util.concurrent.TimeUnit;

      import org.junit.Test;

      public class ShowInetAddress {

      public void main(String[] args) {
      try {
      System.out.println(InetAddress.getLocalHost());
      } catch (UnknownHostException ex) {
      ex.printStackTrace();
      }

      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      put the actual computer name as first alias for 127.0.0.1 (e.g. write it before localhost) in the /etc/hosts file

            robm Robert Mckenna
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: