Java Utililty Methods Local Address Get

List of utility methods to do Local Address Get

Description

The list of methods to do Local Address Get are organized into topic(s).

Method

ListgetLocalInet4Address()
get Local Inet Address
ArrayList<InetAddress> ips = new ArrayList<>();
try {
    Enumeration<NetworkInterface> netIntf = NetworkInterface.getNetworkInterfaces();
    while (netIntf.hasMoreElements()) {
        Enumeration<InetAddress> addresses = netIntf.nextElement().getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress ip = addresses.nextElement();
            if (ip instanceof Inet4Address && ip.isSiteLocalAddress())
...
StringgetLocalInternetProtocolAddress()
get Local Internet Protocol Address
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
    for (InterfaceAddress interfaceAddress : networkInterfaces.nextElement().getInterfaceAddresses()) {
        if (interfaceAddress.getAddress().isSiteLocalAddress()) {
            return interfaceAddress.getAddress().toString().replace("/", "");
throw new IllegalStateException("Expected the computer's local IP address but didn't get one!");
StringgetLocalMachineAddress()
get Local Machine Address
try {
    String hostName = InetAddress.getLocalHost().getHostName();
    InetAddress addrs[] = InetAddress.getAllByName(hostName);
    for (InetAddress addr : addrs) {
        if (!addr.isLoopbackAddress() && addr.isSiteLocalAddress()) {
            return addr.getHostAddress();
    return InetAddress.getLocalHost().getHostAddress();
} catch (Exception e) {
return "Unknown";
InetAddressgetLocalNetAddress()
get Local Net Address
try {
    return getNet4Address();
} catch (Exception ex) {
return null;