How to retrieve the IP address of a Linux system in Jav…

To obtain the IP address of a Linux system in Java, you can use the methods provided by the InetAddress class. Below is an example code for retrieving the local IP address:

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

public class Main {
    public static void main(String[] args) {
        try {
            InetAddress localhost = InetAddress.getLocalHost();
            System.out.println("IP Address: " + localhost.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

This code segment utilizes the getLocalHost() method to retrieve the InetAddress object of the local host, and then uses the getHostAddress() method to obtain the IP address.

bannerAds