Paul Osborne - Java/PHP Developer

Getting the local host name using Java

A quick example of how to get the name of the localhost using the Java InetAddress class.


 String hostname = null;
 try {
     InetAddress localhost = Address.getLocalHost();
     if (localhost != null) {
          hostname = localhost.getHostName();
     }
 } catch (UnknownHostException e) {
      e.printStackTrace();
 }
 System.out.println("Hostname is " + (hostname != null ? hostname : "Unknown") );

Post a Comment

Your email is never shared. Required fields are marked *

*
*