Here you can find the source of getLocalHostName()
public static String getLocalHostName()
//package com.java2s; /*/*from w ww . jav a 2 s.com*/ * NetUtils.java * * This file is part of the IHMC Util Library * Copyright (c) 1993-2016 IHMC. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * version 3 (GPLv3) as published by the Free Software Foundation. * * U.S. Government agencies and organizations may redistribute * and/or modify this program under terms equivalent to * "Government Purpose Rights" as defined by DFARS * 252.227-7014(a)(12) (February 2014). * * Alternative licenses that allow for use within commercial products may be * available. Contact Niranjan Suri at IHMC (nsuri@ihmc.us) for details. */ import java.net.InetAddress; import java.net.UnknownHostException; public class Main { /** * Best attempt to get the local hostname, with or without the * existence of a DNS entry */ public static String getLocalHostName() { String localHostName = null; try { InetAddress addr = InetAddress.getLocalHost(); localHostName = addr.getHostName(); } catch (UnknownHostException uhe) { String host = uhe.getMessage(); if (host != null) { return host.substring(0, host.indexOf(':')); } return null; } return localHostName; } }