Latest web development tutorials

Java Examples - access to the local ip address and host name

Java Examples Java Examples

The following example demonstrates how to use InetAddress class getLocalAddress () method to get the local ip address and hostname:

/*
 author by w3cschool.cc
 Main.java
 */

import java.net.InetAddress;

public class Main {
   public static void main(String[] args) 
   throws Exception {
      InetAddress addr = InetAddress.getLocalHost();
      System.out.println("Local HostAddress: 
      "+addr.getHostAddress());
      String hostname = addr.getHostName();
      System.out.println("Local host name: "+hostname);
   }
}

The above code is run output is:

Local HostAddress: 192.168.1.4
Local host name: harsh

Java Examples Java Examples