How do I get IP address of localhost?

Category: java.net, viewed: 18K time(s).

The example here show you how to get a text representation of an InetAddress IP or host address.

package org.kodejava.example.net;

import java.net.InetAddress;
import java.net.UnknownHostException;

public class IPAddressExample
{
    public static void main(String[] args)
    {
        try
        {
            InetAddress address = InetAddress.getLocalHost();
            String ip = address.getHostAddress();

            System.out.println("IP Address = " + ip);
        } catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
    }
}
Powered by Disqus