在Java中获取FQDN
我正在尝试使用 Java 代码获取我的域中 Windows 计算机的 FQDN。
我尝试了 InetAddress.getByName("machine-1").getCanonicalHostName()
但只返回计算机名称。
另一方面,如果我 ping“machine-1”,我会得到完整的域名。
你知道该怎么做吗?
I am trying to obtain the FQDN of a windows machine in my domain, using Java code.
I have tried the InetAddress.getByName("machine-1").getCanonicalHostName()
but only returns the machine name.
On the other hand if I ping "machine-1" I get the fully domain name.
Do you know how to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
简单的答案是,如果可以的话,您的建议就会有效。
API 确实声明,如果可以的话,它将返回 FQDN。这取决于系统配置。
您发布的代码确实适用于 Windows 域计算机上的我,但我不能说为什么它不适合您。
如果您无法更改机器/域配置,以便 java 可以拾取它,并且您的代码必须使用该 FQDN,则您可以诉诸于从 java 执行 ping 命令并至少将结果解析为临时的措施。
The simple answer is that what you suggest works if it can.
The API does state that it will return the FQDN if it can. This depends on the system configuration.
The code you post does work for me on a windows domain machine, but I can't say why it wouldn't for you.
If you are unable to alter the machine / domain configuration such that java can pick it up, and it is essential for your code to use that FQDN, you could resort to executing the ping command from java and parse the results at least as a temporary measure.
超级晚回复,也许会对下一个旅行者有所帮助。
这将返回 FQDN - 我的 JVM 版本是 1.8.0_144
我发现这个 JDK bug 报告 http ://bugs.java.com/view_bug.do?bug_id=7166687 这可能可以解释为什么有如此多的混乱。
现在仅返回主机名。
Super late reply, perhaps it will help the next traveler.
This will return the FQDN - My JVM version is 1.8.0_144
I found this JDK bug report http://bugs.java.com/view_bug.do?bug_id=7166687 which might explain why there is so much confusion.
This returns just the host name now.
另一个迟到的答复,但我今天也需要这个,并且调用
getCanonicalHostName
的答案对我来说太慢了,它似乎需要 DNS 查找。如果您不介意使用 JNA(我的项目中已经需要它),这将非常快地为您完成(仅适用于 Windows):
Another late reply, but I needed this today also and the answer to call
getCanonicalHostName
was far too slow for me, it seems to require a DNS lookup.If you don't mind using JNA (I already required it in my project) this will do it for you very fast (for Windows only):