Created
January 13, 2020 10:37
-
-
Save rifttech/1eb88cf231a327d48580fa8a8eece987 to your computer and use it in GitHub Desktop.
Get Hostname
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.net.InetAddress; | |
| import java.net.UnknownHostException; | |
| // try InetAddress.LocalHost first; | |
| // NOTE -- InetAddress.getLocalHost().getHostName() will not work in certain environments. | |
| try { | |
| String result = InetAddress.getLocalHost().getHostName(); | |
| if (StringUtils.isNotEmpty( result)) | |
| return result; | |
| } catch (UnknownHostException e) { | |
| // failed; try alternate means. | |
| } | |
| // try environment properties. | |
| // | |
| String host = System.getenv("COMPUTERNAME"); | |
| if (host != null) | |
| return host; | |
| host = System.getenv("HOSTNAME"); | |
| if (host != null) | |
| return host; | |
| // undetermined. | |
| return null; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment