Here you can find the source of getIpAddressesFromNic()
public static InetAddress getIpAddressesFromNic()
//package com.java2s; /*//w w w . j a v a 2s .c o m * Copyright 2015 dorkbox, llc * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.net.*; import java.util.Enumeration; public class Main { /** * Tries to retrieve the IP address from the NIC. Order is ETHx -> EMx -> WLANx * * @return null if not found */ public static InetAddress getIpAddressesFromNic() { try { Enumeration<NetworkInterface> nets = NetworkInterface.getNetworkInterfaces(); while (nets.hasMoreElements()) { NetworkInterface nextElement = nets.nextElement(); String name = nextElement.getName(); // we only want to use ethX addresses! if (name.startsWith("eth")) { Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress address = inetAddresses.nextElement(); // we only support IPV4 addresses. if (!address.isLoopbackAddress() && address instanceof Inet4Address) { return address; } } } } // it didn't work with ethX, so try emX! while (nets.hasMoreElements()) { nets = NetworkInterface.getNetworkInterfaces(); NetworkInterface nextElement = nets.nextElement(); String name = nextElement.getName(); // we only want to use emX addresses! if (name.startsWith("em")) { Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress address = inetAddresses.nextElement(); // we only support IPV4 addresses. if (!address.isLoopbackAddress() && address instanceof Inet4Address) { return address; } } } } // it didn't work with emX, so try enX! while (nets.hasMoreElements()) { nets = NetworkInterface.getNetworkInterfaces(); NetworkInterface nextElement = nets.nextElement(); String name = nextElement.getName(); // we only want to use enX addresses! if (name.startsWith("en")) { Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress address = inetAddresses.nextElement(); // we only support IPV4 addresses. if (!address.isLoopbackAddress() && address instanceof Inet4Address) { return address; } } } } // it didn't work with ethX, so try wifi! while (nets.hasMoreElements()) { nets = NetworkInterface.getNetworkInterfaces(); NetworkInterface nextElement = nets.nextElement(); String name = nextElement.getName(); // we only want to use wlanX addresses! if (name.startsWith("wlan")) { Enumeration<InetAddress> inetAddresses = nextElement.getInetAddresses(); while (inetAddresses.hasMoreElements()) { InetAddress address = inetAddresses.nextElement(); // we only support IPV4 addresses. if (!address.isLoopbackAddress() && address instanceof Inet4Address) { return address; } } } } } catch (SocketException ignored) { } return null; } }