Here you can find the source of getHostIp()
public static String getHostIp()
//package com.java2s; /*//w w w . j ava2 s .co m * Biocep: R-based Platform for Computational e-Science. * * Copyright (C) 2007-2009 Karim Chine - karim.chine@m4x.org * * Copyright (C) 2007 EMBL-EBI-Microarray Informatics * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; import java.util.Vector; public class Main { public static final String UNKOWN = "Unknown"; private static String _hostIp = null; public static String getHostIp() { if (_hostIp == null) { try { if (System.getProperty("host.ip.forced") != null && !System.getProperty("host.ip.forced").equals("")) { _hostIp = System.getProperty("host.ip.forced"); } else { //if (isAmazonCloud()) { // _hostIp=getAMIHostIp(); //} else { if (publicIPUnavilable()) { String IPAddressFromNetworkInterfaces = getIPAddressFromNetworkInterfaces(); if (IPAddressFromNetworkInterfaces != null) _hostIp = IPAddressFromNetworkInterfaces; else _hostIp = "127.0.0.1"; } else { _hostIp = InetAddress.getLocalHost().getHostAddress(); } } } } catch (Exception e) { e.printStackTrace(); _hostIp = UNKOWN; } System.out.println("$$$ HOST IP:" + _hostIp); } return _hostIp; } public static boolean publicIPUnavilable() { try { return isLoopBackIP(InetAddress.getLocalHost().getHostAddress()); } catch (Exception e) { e.printStackTrace(); return true; } } public static String getIPAddressFromNetworkInterfaces() { Vector<String> IPs = new Vector<String>(); try { NetworkInterface iface = null; for (Enumeration<?> ifaces = NetworkInterface.getNetworkInterfaces(); ifaces.hasMoreElements();) { iface = (NetworkInterface) ifaces.nextElement(); InetAddress ia = null; for (Enumeration<?> ips = iface.getInetAddresses(); ips.hasMoreElements();) { ia = (InetAddress) ips.nextElement(); boolean matches = isValidIPAddress(ia.getHostAddress()) && !isLoopBackIP(ia.getHostAddress()); if (matches) { IPs.add(ia.getHostAddress()); } } } } catch (Exception e) { e.printStackTrace(); } if (IPs.size() == 1) return IPs.elementAt(0); return null; } public static boolean isLoopBackIP(String ipAddress) { return ipAddress.startsWith("127."); } public static boolean isValidIPAddress(String ipAddress) { String[] parts = ipAddress.split("\\."); if (parts.length != 4) { return false; } for (String s : parts) { int i = Integer.parseInt(s); if ((i < 0) || (i > 255)) { return false; } } return true; } }