How to Get Current IP in Java

You can use the getLocalHost() method in the InetAddress class to retrieve the current IP address. Here is an example of the code:

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

public class GetIPAddress {
    public static void main(String[] args) {
        try {
            InetAddress address = InetAddress.getLocalHost();
            System.out.println("当前IP地址:" + address.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}

Execution result:

当前IP地址:192.168.0.100
bannerAds