hostname

Recommended way to get hostname in Java

梦想与她 提交于 2019-11-25 20:24:50
Which of the following is the best and most portable way to get the hostname of the current computer in Java? Runtime.getRuntime().exec("hostname") vs InetAddress.getLocalHost().getHostName() Strictly speaking - you have no choice but calling either hostname(1) or - on Unix gethostname(2) . This is the name of your computer. Any attempt to determine the hostname by an IP address like this InetAddress.getLocalHost().getHostName() is bound to fail in some circumstances: The IP address might not resolve into any name. Bad DNS setup, bad system setup or bad provider setup may be the reason for