Monday, 12 October 2015

Java code to get system IP address (IPv4 Address)

Requirement: I want to get the IP address (IPv4 address ) of my system.

Solution: The Java code for the above requirement is shown below:

Program:

package com.susanto;

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

public class GetSystemIP {
      public static void main(String[] args) throws UnknownHostException {
            InetAddress address1 = InetAddress.getLocalHost();
            String ipAddress = address1.toString().substring(
                        address1.toString().lastIndexOf("/") + 1);
            System.out.println("IP Address is : " + ipAddress);
      }
}

Output:

IP Address is : 10.100.160.22

Hence, the solution to our requirement.


Thanks & Regards,
Susanto Paul