Skip to content

Instantly share code, notes, and snippets.

@yyunikov
Created January 15, 2015 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yyunikov/f72744845796a61991a4 to your computer and use it in GitHub Desktop.
Save yyunikov/f72744845796a61991a4 to your computer and use it in GitHub Desktop.
Getting machine's hostname
public class SystemUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(SystemUtils.class);
private static final String HOSTNAME_VARIABLE = "HOSTNAME";
private static final String HOSTNAME_COMMAND = "hostname";
private SystemUtils() {}
public static String getHostname() throws IOException {
final String hostname = System.getenv(HOSTNAME_VARIABLE);
if (hostname == null) {
final Process p = Runtime.getRuntime().exec(HOSTNAME_COMMAND);
final BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
final StringBuilder outputBuilder = new StringBuilder();
String line;
while ((line = in.readLine()) != null) {
outputBuilder.append(line);
}
LOGGER.debug("Returning output from executing \'hostname\' command");
return outputBuilder.toString();
}
LOGGER.debug("Returning hostname from the HOSTNAME environment variable");
return hostname;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment