Java Program to Get System IP Address in Windows and Linux Machine


IP Address, also known as the Internet Protocol Address is a unique identifier that is assigned to a device in a network to identify the devices in the network and establish connection between them. In this section, we will learn how to find the IP Address of a Windows and Linux Machine using Java Code.

IP address can be represented in two formats. One format is IPv4 which is a 32 format containing decimals and dots. It has two parts, one part is network ID which identifies network and the other is host ID which identifies the device in the network. The other format is IPv6 which is 128-bit format which is represented by hexadecimal digits.

Now, we will look into detail about the different methods used to find the IP Address of Linux and Windows machines. Java Provides InetAddress class and NetworkInterface class to perform the operations on IP Address.

Now, we will look into different code implementations for finding the IP address of Windows and Linux Machines.

Approach 1: Using InetAddress.getLocalHost()

In this approach, we will implement java code to find the IP Address of a Machine using ‘InetAddress.getLocalHost()’ method and ‘getHostAddress()’ method .

InetAddress.getLocalHost() method is used to get the IP address and host name of the current machine.It returns an instance of the InetAddress class, which represents an Internet Protocol (IP) address. The getLocalHost() method can be used to retrieve the IP address of the local host in either its string form or its byte array form.

The algorithm to find the IP Address is as below.

Algorithm

  • Get IP address using InetAddress.getLocalHost().

  • Print the IP address.

  • If an exception is caught, print Could not find IP address for this host.

Example

In this example, we initially get a InetAddress Object using ‘InetAddress.getLocalHost()’ method then we used the getHostAddress() method on the InetAddress Object and we then print the value of IP Address of the machine.

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

public class Main {
   public static void main(String[] args) {
      try {
         InetAddress address = InetAddress.getLocalHost();
         System.out.println("IP address: " + address.getHostAddress());
      } catch (UnknownHostException ex) {
         System.out.println("Could not find IP address for this host");
      }
   }
}

Output

IP address: 172.17.0.2

Approach 2: Using NetworkInterface.getNetworkInterfaces()

In this approach, we will implement java code to find the IP Address of a Machine using ‘NetworkInterface.getNetworkInterfaces()’ method. The algorithm to find the IP Address is as below.

Algorithm

  • Get an enumeration of network interfaces using NetworkInterface.getNetworkInterfaces().

  • While the enumeration has more elements,for each network check if the network is up and loopback using networkInterface.isUp() and networkInterface.isLoopback().

  • If network is valid,get IP address for each of the network and check if IP address is site local address or link local address using address.isLinkLocalAddress() and address.isSiteLocalAddress().

  • If an exception is caught, print the trace.

Example

In this example, we use ‘getNetworkInterfaces()’ method to retrieve the enumeration of NetworkInterfaces. We iterate over each interface and check whether the interface is up using ‘networkInterface.isUp()’ method if the returned value by this method is false then we just pass the interface and move on to next one as we now need only the IP addresses which are currently active. We also check whether it interacts with the external network using ‘networkInterface.isLoopback()’ on each Network Interface, if this method returns true then that particular interface doesn’t interact with external networks. So, we skip printing those kind of networks. Then, if the above two tests are passed by Network Interface then we get enumeration of InetAddress using ‘getInetAddresses()’ method on that particular Interface.

We again check on each InetAddress whether it is link local Address using ‘isLinkLocalAddress()’ method, if this method returns true then that particular address is local one and it is not routable to external network. So, we pass this kind of Ip addresses as they are invalid for communication. At last, we check whether the address is Site local address using ‘isSiteLocalAddress()’ method if that particular address is site local address then the method returns true it implies that the particular address is a valid IP address for communication. So, we will print that particular address using ‘getHostAddress()’ method.

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

public class Main {
   public static void main(String[] args) {
      try {
         Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
         while (networkInterfaces.hasMoreElements()) {
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            if (!networkInterface.isUp()) {
               continue;
            }
            if (networkInterface.isLoopback()) {
               continue;
            }
            Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
            while (addresses.hasMoreElements()) {
               InetAddress address = addresses.nextElement();
               if (address.isLinkLocalAddress()) {
                  continue;
               }
               if (address.isSiteLocalAddress()) {
                     System.out.println("IP address: " + address.getHostAddress());
               }
            }
         }
      } catch (SocketException ex) {
         ex.printStackTrace();
      }
   }
}

Output

IP address: 172.17.0.2

Thus, in this article we have discussed how to get System IP Address in Windows and Linux machines.

Updated on: 16-Aug-2023

339 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements