Java API Tutorial - Java InetAddress.getLocalHost()








Syntax

InetAddress.getLocalHost() has the following syntax.

public static InetAddress getLocalHost()    throws UnknownHostException

Example

In the following code shows how to use InetAddress.getLocalHost() method.

/* w  w  w.j  av a2  s.com*/

import java.net.InetAddress;

public class Main {

  public static void main(String[] args) {

    try {
      InetAddress me = InetAddress.getLocalHost();
      String dottedQuad = me.getHostAddress();
      System.out.println("My address is " + dottedQuad);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}

The code above generates the following result.