get Local Host IP - Java Network

Java examples for Network:IP Address

Description

get Local Host IP

Demo Code


//package com.java2s;
import java.net.InetAddress;
import java.net.UnknownHostException;

public class Main {
    public static void main(String[] argv) throws Exception {
        System.out.println(getLocalHostIP());
    }//w w w .  ja va  2s. co m

    public static String getLocalHostIP() {
        try {
            InetAddress addr = InetAddress.getLocalHost();
            return addr.getHostAddress();
        } catch (UnknownHostException e) {
            throw new RuntimeException(
                    "it can't get local host ip error as :", e);
        }
    }
}

Related Tutorials