title: Java local hostname ip resolution
body: <p>I am running into a weird issue on my macbook pro where hostname to ip resolution. Here is the code,  </p>
  
  <pre><code>import java.net.InetAddress;
  
  public class IpRes {
    public static void main(String[] args) throws Exception {
      String host = args[0];
      InetAddress address = InetAddress.getByName(host);
      System.out.println(address.getHostAddress());
    }
  }
  </code></pre>
  
  <p>My host name is "a-b-vik". I have entries in /etc/hosts for</p>
  
  <pre><code>127.0.0.1 localhost
  127.0.0.1 a-b-vik 
  </code></pre>
  
  <p>Here is output when I run the above program,</p>
  
  <pre><code>java IpRes "localhost"
  127.0.0.1
  
  java IpRes "a-b-vik"
  10.0.0.4
  </code></pre>
  
  <p>Why is the hostname not resolving to '127.0.0.1'? I tried with -Djava.net.preferIPv4Stack=true and get the same result. Also I tried writing a C program and there it seems to give 127.0.0.1. So this seems to be a jvm specific issue.</p>
  
  <p>What is weird is that I able to get it to work if I use a '\' anywhere in my hostname. For example,</p>
  
  <pre><code>java IpRes "a-\b-vik'
  127.0.0.1
  </code></pre>
  
  <p>The same program with same /etc/hosts file works from my friend's mac. Not sure why I am facing this issue. Any ideas?</p>