title: How do I get the local hostname if unresolvable through DNS in Java?
body: <p>This sounds like something that should have been asked before, and it has sort of, but I'm looking to get the local hostname and IP addresses of a machine even when it is not resolvable through DNS (in Java). </p>
  
  <p>I can get the local IP addresses without resolution by iterating through <code>NetworkInterfaces.getNetworkInterfaces()</code>.  </p>
  
  <p>Any answers to this question I've found indicate to use getLocalHost() </p>
  
  <pre><code>InetAddress localhost = java.net.InetAddress.getLocalHost();
  hostName = localhost.getHostName();
  </code></pre>
  
  <p>but this throws an <code>UnknownHostException</code> if the hostname isn't resolvable through DNS.  </p>
  
  <p>Is there no way to get the local hostname without a DNS lookup happening behind the scenes?</p>
  
  <p><strong>edit:</strong> the IP address retrieved is 10.4.168.23
  The exception is <code>java.net.UnknownHostException: cms1.companyname.com: cms1.companyname.com</code> (hostname changed for pseudo-anonymity), and the hosts file does not contain the hostname.  But it does <em>know</em> its hostname, so I'm not sure why I can't get it without an exception being thrown.</p>