简体   繁体   English

在最新的jdk7中,使用java获取主机名失败

[英]Getting hostname with java fails in latest jdk7

I've been getting hostname of the machine as follows: 我一直在获得机器的主机名如下:

InetAddress.getLocalHost().getHostName();

However, when I put latest JDK (jdk1.7.0_04), the code above simply return LOCALHOST. 但是,当我放入最新的JDK(jdk1.7.0_04)时,上面的代码只返回LOCALHOST。 I checked /etc/hosts (its linux) and it says there: 我检查了/ etc / hosts(它的linux),它说:

127.0.0.1    localhost    redbull

It's been returning REDBULL until upgrade. 在升级之前它一直在返回REDBULL。 So I changed that around putting 所以我把它改成了

127.0.0.1    redbull    localhost

instead and it started returning REDBULL without a problem. 相反,它开始返回REDBULL没有问题。

Is there a better way of making this work? 是否有更好的方法使这项工作?

Well, I thought about flagging this as a dup, but the only answers I find suggest that you use InetAddress.getLocalHost().getHostName() . 好吧,我InetAddress.getLocalHost().getHostName()它标记为重复,但我发现的唯一答案建议您使用InetAddress.getLocalHost().getHostName() Which, frankly, I think should return "localhost" in this case. 坦率地说,在这种情况下,我认为应该返回“localhost”。 And those answers, I suppose, are correct, in that there's really no pure Java way of doing this (at least none that are portable back to older JREs.) 我认为这些答案是正确的,因为实际上没有纯Java方法(至少没有一种可以移植到旧JRE中)。

We use JNI to accomplish this. 我们使用JNI来实现这一目标。 We call SCPreferencesGetHostName() on Mac OS 10.4+, SCDynamicStoreCopyLocalHostName() on older Mac OS, GetComputerName() on Win32, gethostname() everywhere else. 我们称SCPreferencesGetHostName()在Mac OS 10.4+, SCDynamicStoreCopyLocalHostName()在旧的Mac OS, GetComputerName()在Win32, gethostname()其他地方。

You could, of course, simply call /bin/hostname on Unix machines or look at the environment variable COMPUTERNAME on Windows. 当然,您可以在Unix机器上调用/bin/hostname ,或者在Windows上查看环境变量COMPUTERNAME It's sort of a judgement call as to whether you feel better calling JNI or exec ing another program. 对于你是否感觉更好地召唤JNI或exec另一个程序,这是一种判断。

For what it's worth, the reason we don't call gethostname() on Mac OS is because Mac does a weird dynamic hostname thing, where gethostname() will return the reverse DNS of your primary ethernet device. 对于它的价值,我们不在Mac OS上调用gethostname()的原因是因为Mac做了一个奇怪的动态主机名, gethostname()将返回主以太网设备的反向DNS。 If I were to plug my Mac straight into my cable modem, I'd get a hostname of customer-10-42-21-42 or whatever my cable provider decided to set as my PTR record in their DNS. 如果我将我的Mac直接插入我的电缆调制解调器,我会得到一个customer-10-42-21-42主机名customer-10-42-21-42或我的有线电视提供商决定在他们的DNS中设置为我的PTR记录。 Instead, going to the preferences will get you a stable hostname that was determined by the user. 相反,转到首选项将获得由用户确定的稳定主机名。

I had the same problem and when all the following lined up it worked. 我遇到了同样的问题,当以下所有内容排成一行时都有效。 host name had to be appended with DOT local 主机名必须附加DOT本地

$ scutil --get HostName
drums
$ scutil --get LocalHostName
drums
$ scutil --get ComputerName
drums

$ sudo hostname drums.local
$ hostname
drums.local

$sudo vim /etc/hosts
192.168.x.IP drums
127.0.0.1 localhost drums
255.255.255.255 broadcasthost
::1 localhost
fXXX::1XXX localhost

$networksetup -setv6off Ethernet

$ sw_vers
ProductName: Mac OS X
ProductVersion: 10.9

$ java -version
java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

This is a known issue in the macosx port of JDK7u4. 这是JDK7u4的macosx端口中的已知问题。

http://java.net/jira/browse/MACOSX_PORT-564 http://java.net/jira/browse/MACOSX_PORT-564

If you're not against using an external dependency from maven central, I wrote gethostname4j to solve this problem for myself. 如果你不反对使用maven central的外部依赖,我写了gethostname4j来解决这个问题。 It just uses JNA to call libc's gethostname function (or gets the ComputerName on Windows) and returns it to you as a string. 它只是使用JNA调用libc的gethostname函数(或在Windows上获取ComputerName)并将其作为字符串返回给您。

https://github.com/mattsheppard/gethostname4j https://github.com/mattsheppard/gethostname4j

@edward-thomson's answer above makes me thing I might have a bit more work to do to make it work well on MacOS though :) @ edward-thomson上面的回答让我觉得我可能需要做更多工作才能让它在MacOS上运行良好:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM