Skip to content

Instantly share code, notes, and snippets.

@rifttech
Created January 13, 2020 10:37
Show Gist options
  • Select an option

  • Save rifttech/1eb88cf231a327d48580fa8a8eece987 to your computer and use it in GitHub Desktop.

Select an option

Save rifttech/1eb88cf231a327d48580fa8a8eece987 to your computer and use it in GitHub Desktop.
Get Hostname
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