Java – why InetAddress getLocalHost(). Gethostname() returns a value different from bash “hostname”?
I have a build The gradle task works like a champion on my development box, generating a properties file that records the name of the generated machine The logic is simple
def hostname = InetAddress.getLocalHost().getHostName();
On my development box, this always produces the same value as the hostname from the bash shell
bobk-mbp:DM_Server bobk$hostname bobk-mbp.local
However, on our Jenkins CI server, the bash hostname returned something, but I called InetAddress getLocalHost(). getHostName(); Return to something else What does the Jenkins machine need to change to restore the same value to both machines?
Solution
Suppose you are on Linux and return the hostname of the kernel configuration from the hostname command executed by O / s
InetAddress. Gethostname() uses the naming service (DNS) configured in your o / s to reverse lookup the IP address of the server
If you need the hostname understood by O / s, you can use system Getenv gets the hostname from the environment variable, which is probably the simplest option This is not a completely reliable method, but it may be sufficient without going deep into the network or system administrator