下面哪一种方法是在Java中获得当前计算机主机名的最佳和最可移植的方法?

Runtime.getRuntime().exec(“hostname”)

vs

InetAddress.getLocalHost().getHostName()


当前回答

hostName == null;
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
{
    while (interfaces.hasMoreElements()) {
        NetworkInterface nic = interfaces.nextElement();
        Enumeration<InetAddress> addresses = nic.getInetAddresses();
        while (hostName == null && addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (!address.isLoopbackAddress()) {
                hostName = address.getHostName();
            }
        }
    }
}

其他回答

InetAddress.getLocalHost().getHostName()是更好的(由Nick解释),但仍然不是很好

一个主机可以使用许多不同的主机名。通常,您将在特定的上下文中查找主机名。

例如,在web应用程序中,您可能要查找发出您当前正在处理的请求的人所使用的主机名。如何最好地找到它取决于你的web应用程序使用的框架。

在其他一些面向互联网的服务中,你会希望你的服务从“外部”可用的主机名。由于代理、防火墙等原因,这甚至可能不是安装服务的机器上的主机名——你可能会尝试提出一个合理的默认值,但你一定要让安装它的人都可以配置它。

InetAddress.getLocalHost(). gethostname()是更可移植的方式。

Exec(“hostname”)实际上调用操作系统来执行hostname命令。

以下是关于SO的一些其他相关答案:

Java当前机器名和登录用户? 获取本地机器的DNS名称,就像远程机器看到的那样

编辑:你应该看看A.H.的回答,或者Arnout Engelen的回答,详细说明为什么这可能不会像预期的那样工作,这取决于你的情况。作为对这个特别要求可移植的人的回答,我仍然认为getHostName()很好,但它们提出了一些值得考虑的优点。

在Java中获取当前计算机主机名的最方便的方法如下:

import java.net.InetAddress;
import java.net.UnknownHostException;

public class getHostName {

    public static void main(String[] args) throws UnknownHostException {
        InetAddress iAddress = InetAddress.getLocalHost();
        String hostName = iAddress.getHostName();
        //To get  the Canonical host name
        String canonicalHostName = iAddress.getCanonicalHostName();

        System.out.println("HostName:" + hostName);
        System.out.println("Canonical Host Name:" + canonicalHostName);
    }
}

严格地说,在Unix上,你没有选择,只能调用hostname(1)或- gethostname(2)。这是你电脑的名字。任何试图通过IP地址确定主机名的尝试,就像这样

InetAddress.getLocalHost().getHostName()

在某些情况下一定会失败:

The IP address might not resolve into any name. Bad DNS setup, bad system setup or bad provider setup may be the reason for this. A name in DNS can have many aliases called CNAMEs. These can only be resolved in one direction properly: name to address. The reverse direction is ambiguous. Which one is the "official" name? A host can have many different IP addresses - and each address can have many different names. Two common cases are: One ethernet port has several "logical" IP addresses or the computer has several ethernet ports. It is configurable whether they share an IP or have different IPs. This is called "multihomed". One Name in DNS can resolve to several IP Addresses. And not all of those addresses must be located on the same computer! (Usecase: A simple form of load-balancing) Let's not even start talking about dynamic IP addresses.

另外,不要将ip地址的名称与主机的名称(hostname)混淆。打个比方可能会更清楚:

There is a large city (server) called "London". Inside the city walls much business happens. The city has several gates (IP addresses). Each gate has a name ("North Gate", "River Gate", "Southampton Gate"...) but the name of the gate is not the name of the city. Also you cannot deduce the name of the city by using the name of a gate - "North Gate" would catch half of the bigger cities and not just one city. However - a stranger (IP packet) walks along the river and asks a local: "I have a strange address: 'Rivergate, second left, third house'. Can you help me?" The local says: "Of course, you are on the right road, simply go ahead and you will arrive at your destination within half an hour."

我认为这很好地说明了这一点。

好消息是:真实的主机名通常是不必要的。在大多数情况下,这台主机上任何解析为IP地址的名称都可以。(陌生人可能会从北门(Northgate)进入城市,但乐于助人的当地人会翻译“左边第二个”部分。)

在其余的情况下,您必须使用该配置设置的最终来源-这是C函数gethostname(2)。该函数也由程序主机名调用。

hostName == null;
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
{
    while (interfaces.hasMoreElements()) {
        NetworkInterface nic = interfaces.nextElement();
        Enumeration<InetAddress> addresses = nic.getInetAddresses();
        while (hostName == null && addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            if (!address.isLoopbackAddress()) {
                hostName = address.getHostName();
            }
        }
    }
}