Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting hostname with java fails in latest jdk7

Tags:

java

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. I checked /etc/hosts (its linux) and it says there:

127.0.0.1    localhost    redbull

It's been returning REDBULL until upgrade. So I changed that around putting

127.0.0.1    redbull    localhost

instead and it started returning REDBULL without a problem.

Is there a better way of making this work?

like image 283
Daniil Avatar asked May 10 '12 21:05

Daniil


3 Answers

Well, I thought about flagging this as a dup, but the only answers I find suggest that you use InetAddress.getLocalHost().getHostName(). Which, frankly, I think should return "localhost" in this case. 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.)

We use JNI to accomplish this. We call SCPreferencesGetHostName() on Mac OS 10.4+, SCDynamicStoreCopyLocalHostName() on older Mac OS, GetComputerName() on Win32, gethostname() everywhere else.

You could, of course, simply call /bin/hostname on Unix machines or look at the environment variable COMPUTERNAME on Windows. It's sort of a judgement call as to whether you feel better calling JNI or execing another program.

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. 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. Instead, going to the preferences will get you a stable hostname that was determined by the user.

like image 80
Edward Thomson Avatar answered Nov 11 '22 01:11

Edward Thomson


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

@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 :)

like image 44
Matt Sheppard Avatar answered Nov 10 '22 23:11

Matt Sheppard


I had the same problem and when all the following lined up it worked. host name had to be appended with DOT local

$ 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)
like image 2
user2980828 Avatar answered Nov 11 '22 01:11

user2980828