HS Banner
Back
Get IP Address

Author: Admin 03/26/2021
Language: Java
Tags:


Description:

How to get my machine or local host IP address in Java.

Article:

Below example shows how to get IP address of a host or machine. You can get it by using InetAddress class. getLocalHost() method returns the information about the host, and returns InetAddress object. If you call getHostAddress() method, you can get IP address of the host.

package com.myjava.ip;
 
import java.net.InetAddress;
import java.net.UnknownHostException;
 
public class MyIpAddress {
 
    public static void main(String a[]){
     
        try {
            InetAddress ipAddr = InetAddress.getLocalHost();
            System.out.println(ipAddr.getHostAddress());
        } catch (UnknownHostException ex) {
            ex.printStackTrace();
        }
    }
}


Back
Comments
Add Comment
There are no comments yet.