Java Utililty Methods IP Address Get

List of utility methods to do IP Address Get

Description

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

Method

StringgetIpAddress()
get Ip Address
try {
    return InetAddress.getLocalHost().getHostAddress();
} catch (Exception ex) {
return null;
StringgetIpAddress()
Return a string representation of this machine's IP address, or "unknown" if unable to retrieve the address.
try {
    java.net.InetAddress addr = java.net.InetAddress.getLocalHost();
    return addr.getHostAddress();
} catch (java.net.UnknownHostException ex) {
    return "unknown";
StringgetIPAddress()
get IP Address
InetAddress inetAddress = null;
try {
    inetAddress = InetAddress.getLocalHost();
} catch (UnknownHostException ex) {
return (inetAddress == null ? "<unknown>" : inetAddress.getHostAddress());
StringgetIPAddress()
get IP Address
String ipAddress = "";
try {
    URL whatismyip = new URL("http://icanhazip.com");
    BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream()));
    ipAddress = in.readLine();
    System.out.println(ipAddress);
} catch (Exception e) {
return ipAddress;
StringgetIPAddress()
Get the IP address of the host computer, or the loopback address (127.0.0.1) if the operation fails.
try {
    Enumeration<NetworkInterface> nwEnum = NetworkInterface.getNetworkInterfaces();
    while (nwEnum.hasMoreElements()) {
        NetworkInterface nw = nwEnum.nextElement();
        Enumeration<InetAddress> ipEnum = nw.getInetAddresses();
        while (ipEnum.hasMoreElements()) {
            InetAddress ina = ipEnum.nextElement();
            if ((ina instanceof Inet4Address) && !ina.isLoopbackAddress()) {
...
StringgetIpAddress()
get Ip Address
List<NetworkInterface> networkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
return networkInterfaces.stream().filter(n -> n.getName().startsWith("en") || n.getName().startsWith("eth")
        || n.getName().startsWith("wlan")).filter(n -> {
            try {
                return !n.isLoopback() && n.isUp();
            } catch (SocketException e1) {
                return false;
        }).map(n -> {
            Optional<InetAddress> inetAddress = Collections.list(n.getInetAddresses()).stream()
                    .filter(a -> a instanceof Inet4Address).findFirst();
            if (!inetAddress.isPresent()) {
                inetAddress = Collections.list(n.getInetAddresses()).stream().findFirst();
            return inetAddress.get().getHostAddress();
        }).filter(a -> a != null).findFirst().orElse("127.0.0.1");
StringgetIpAddress()
get Ip Address
Enumeration e = NetworkInterface.getNetworkInterfaces();
String address = "127.0.0.1";
while (e.hasMoreElements()) {
    NetworkInterface netface = (NetworkInterface) e.nextElement();
    Enumeration addresses = netface.getInetAddresses();
    while (addresses.hasMoreElements()) {
        InetAddress ip = (InetAddress) addresses.nextElement();
        if (!ip.isLoopbackAddress() && isIP(ip.getHostAddress())) {
...
StringgetIPAddress()
get IP Address
if (gLocalIPAddress != null)
    return (gLocalIPAddress);
try {
    InetAddress address = InetAddress.getLocalHost();
    return (address.getHostAddress());
} catch (UnknownHostException e) {
    return ("");
StringgetIPAddress()
get IP Address
List<InetAddress> ipAddresses = new ArrayList<InetAddress>();
String ipAddress = null;
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
    NetworkInterface ni = e.nextElement();
    if (ni.isLoopback() || !ni.isUp())
        continue;
    Enumeration<InetAddress> e2 = ni.getInetAddresses();
...
StringgetIPAddress()
Determins the IP address of the machine Kettle is running on.
Enumeration<NetworkInterface> enumInterfaces = NetworkInterface.getNetworkInterfaces();
while (enumInterfaces.hasMoreElements()) {
    NetworkInterface nwi = (NetworkInterface) enumInterfaces.nextElement();
    Enumeration<InetAddress> ip = nwi.getInetAddresses();
    while (ip.hasMoreElements()) {
        InetAddress in = (InetAddress) ip.nextElement();
        if (!in.isLoopbackAddress() && in.toString().indexOf(":") < 0) {
            return in.getHostAddress();
...