问题描述
我正在尝试使用Java获取我的计算机(Windows 7 x64)的全限定名称.在我的机器上,我已经更新了c:\ Windows \ system32 \ drivers \ etc \ hosts文件,使其具有这样的条目:
I am trying get the fully qualified name of my machine (Windows 7 x64) in Java. On my machine, I've updated the c:\Windows\system32\drivers\etc\hosts file such that it has an entry like this:
10.44.2.167 myserver myserver.domain.com
我们所有的系统在\ etc \ hosts文件中都有一个条目(采用上述格式),我无法更改.
All our systems have an entry in the \etc\hosts file (in the above format) which I cannot change.
以下代码始终返回"myserver",而我永远无法获得标准名称.
The following code always returns "myserver" and I am never able to get the fully qualified name.
InetAddress addr = InetAddress.getLocalHost();
String fqName = addr.getCanonicalHostName();
如何在Java中实现这一目标?
How do I achieve this in Java?
谢谢
Shreyas
推荐答案
来自"man hosts"/etc/hosts(或Windows等效)具有以下格式:
from 'man hosts '/etc/hosts (or windows equivalent) has the following format:
ip_address fully_qualified_name aliases
因此,在您的情况下,hosts文件应如下所示:
so in your case, hosts file would look like:
10.44.2.167 myserver.domain.com myserver another_alias
当Java进行主机查找时,如果/etc/hosts有一个条目,它将获取第一个host_name(而不是别名)
When Java does host lookup, if /etc/hosts has an entry, it will grab the first host_name (not the alias)
这篇关于具有/etc/hosts的标准机器名Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!