Skip to content

Instantly share code, notes, and snippets.

@justpsvm

justpsvm/Hostname.java

Last active Jul 24, 2017
Embed
What would you like to do?
[Java 获取主机名] #java
public static String getHostNameForLiunx() {
try {
return (InetAddress.getLocalHost()).getHostName();
} catch (UnknownHostException uhe) {
String host = uhe.getMessage(); // host = "hostname: hostname"
if (host != null) {
int colon = host.indexOf(':');
if (colon > 0) {
return host.substring(0, colon);
}
}
return "UnknownHost";
}
}
public static String getHostName() {
if (System.getenv("COMPUTERNAME") != null) {
return System.getenv("COMPUTERNAME");
} else {
return getHostNameForLiunx();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.