如果Java中的DNS无法解析,如何获取本地主机名?
这听起来像是以前应该问过的问题,而且确实如此,但我希望获取机器的本地主机名和 IP 地址,即使它无法通过 DNS(在 Java 中)解析。
我可以通过迭代 NetworkInterfaces.getNetworkInterfaces()
来获取本地 IP 地址,而无需解析。
我发现的这个问题的任何答案都表明使用 getLocalHost(),
InetAddress localhost = java.net.InetAddress.getLocalHost();
hostName = localhost.getHostName();
但如果主机名无法通过 DNS 解析,则会抛出 UnknownHostException
。
如果没有在幕后进行 DNS 查找,是否无法获取本地主机名?
编辑:检索到的IP地址是10.4.168.23 例外情况是 java.net.UnknownHostException: cms1.companyname.com: cms1.companyname.com
(主机名已更改为伪匿名),并且主机文件不包含主机名。但它确实知道它的主机名,所以我不确定为什么我无法在不抛出异常的情况下获取它。
This sounds like something that should have been asked before, and it has sort of, but I'm looking to get the local hostname and IP addresses of a machine even when it is not resolvable through DNS (in Java).
I can get the local IP addresses without resolution by iterating through NetworkInterfaces.getNetworkInterfaces()
.
Any answers to this question I've found indicate to use getLocalHost()
InetAddress localhost = java.net.InetAddress.getLocalHost();
hostName = localhost.getHostName();
but this throws an UnknownHostException
if the hostname isn't resolvable through DNS.
Is there no way to get the local hostname without a DNS lookup happening behind the scenes?
edit: the IP address retrieved is 10.4.168.23
The exception is java.net.UnknownHostException: cms1.companyname.com: cms1.companyname.com
(hostname changed for pseudo-anonymity), and the hosts file does not contain the hostname. But it does know its hostname, so I'm not sure why I can't get it without an exception being thrown.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
是的,Java 中应该有一种无需借助名称服务查找即可获取主机名的方法,但不幸的是没有。
然而,您可以这样做:
这将让您在传统的 Sun JDK 平台(Windows、Solaris、Linux)上获得您想要的 100% 的结果,但如果您的操作系统更加奇特(从 Java 的角度来看),那么就变得不那么容易了。请参阅代码示例中的注释。
我希望有更好的方法。
Yes, there should be a way in Java to get the hostname without resorting to name service lookups but unfortunately there isn't.
However, you can do something like this:
and that will get you 100% what you want on the traditional Sun JDK platforms (Windows, Solaris, Linux) but becomes less easy if your OS is more excotic (from a Java perspective). See the comments in the code example.
I wish there was a better way.
这个问题很老了,但不幸的是仍然相关,因为在 Java 中获取机器的主机名仍然不是小事。这是我的解决方案,在不同的系统上运行了一些测试:
不同操作系统的结果:
OpenSuse 13.1
Ubuntu 14.04 LTS
这有点奇怪,因为
echo $HOSTNAME
返回正确的主机名,但System.getenv("HOSTNAME")
没有(但这可能只是我的环境的问题) ):Windows 7
机器名称已被替换为(部分)匿名化,但我保留了大小写和结构。请注意执行
hostname
时的额外换行符,在某些情况下您可能需要考虑它。This question is old, but unfortunately still relevant since it's still not trivial to get a machine's host name in Java. Here's my solution with some test runs on different systems:
Results for different operating systems:
OpenSuse 13.1
Ubuntu 14.04 LTS
This one is kinda strange since
echo $HOSTNAME
returns the correct hostname, butSystem.getenv("HOSTNAME")
does not (this however might be an issue with my environment only):Windows 7
The machine names have been replaced for (some) anonymization, but I've kept the capitalization and structure. Note the extra newline when executing
hostname
, you might have to take it into account in some cases.或者,使用 JNA 在 Unix 上调用
gethostname
函数,避免相反的情况DNS 查找。一些注意事项:在 Linux 上,我相信
gethostname
只是调用uname
并解析输出。在 OS X 上,情况更加复杂,因为您的主机名可能会受到 DNS 的影响,但除了这些副作用之外,这绝对是我从hostname
获得的。jna-platform.jar
包含 Win32 函数,因此只需调用Kernel32Util.getComputerName()
即可。Alternatively, use JNA to call the
gethostname
function on unixes, avoiding the reverse DNS lookup.Some notes: on Linux, I believe
gethostname
simply callsuname
and parses the output. On OS X the situation is more complex, as your hostname can be affected by DNS, but those side-effects aside, it's definitely what I get fromhostname
.jna-platform.jar
includes Win32 functions, so there it's as simple as a call toKernel32Util.getComputerName()
.如果您获得 127.0.0.1 作为 IP 地址,那么您可能需要找到操作系统特定的 hosts 文件,并在其中添加到主机名的映射。
If you are getting 127.0.0.1 as the IP address then you may need to locate your Operating System specific hosts file and add a mapping to your hostname in it.
这有点像黑客。但是您可以从 Java 启动一个新进程并运行
hostname
命令。读取子进程的输出流将为您提供本地主机的名称。This is a bit of a hack. But you could launch a new Process from Java and run the
hostname
command. Reading the outputstream of the child process would give you the name of the localhost.如果没有配置 DNS,Java 会读取 /etc/hosts 文件,或者相应的 C 函数会读取。
Java will read the /etc/hosts file if there is no DNS configured, or rather the corresponding C functions will.
在 Linux 上阅读
on Linux read
如果您不反对使用 Maven Central 的外部依赖项,我编写了 gethostname4j 来为自己解决这个问题。它只是使用 JNA 调用 libc 的 gethostname 函数(或在 Windows 上获取 ComputerName)并将其作为字符串返回给您。
https://github.com/mattsheppard/gethostname4j
(我认为这几乎正是@danny-thomas建议,但如果您已经在使用 Maven,可能会更方便;)
If you're not against using an external dependency from maven central, I wrote gethostname4j to solve this problem for myself. It just uses JNA to call libc's gethostname function (or gets the ComputerName on Windows) and returns it to you as a string.
https://github.com/mattsheppard/gethostname4j
(I think it's almost exactly what @danny-thomas proposed, but maybe more convenient if you're already using Maven ;)