Created
January 15, 2015 14:52
-
-
Save yyunikov/f72744845796a61991a4 to your computer and use it in GitHub Desktop.
Getting machine's hostname
This file contains 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
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