Why does InetAddress.getLocalHost().getHostName() return a value different from bash "hostname"?
gradle, java, jenkins
Solution
Assuming you're on linux, the hostname command executed from the o/s returns the kernel's configured hostname.
InetAddress.getHostName() is doing a reverse lookup on the server's IP address using the naming service (DNS) configured in your O/S.
If you need the hostname as understood by the o/s, getting it from an environment variable via System.getenv may be the simplest option. It isn't a completely robust way to do this but it may be enough without needing to delve into network or system admin.
Problem
I've got a build.gradle task that works like a champ on my dev box at producing a properties file that records the name of the machine that the build was generated on. The logic is simple enough... ``` def hostname = InetAddress.getLocalHost().getHostName(); ``` On my dev box this always produces the same value as if I did hostname from the bash shell. ``` bobk-mbp:DM_Server bobk$ hostname bobk-mbp.local ``` On our jenkins CI server, however, bash hostname returns one thing, but my call to InetAddress.getLocalHost().getHostName(); returns something else. What needs to change on the jenkins machine to get these two returning the same value?