TOC
Get the hostname of the machine running Java
InetAddress.getLocalHost().getHostName()
of the machine running in Java.host name(in Japanese history)Machine nameThe following is a sample program to obtain the
The sample program uses the following method to obtain the host name.
- InetAddress.getLocalHost() Represents the local host by InetAddress Obtains an instance of
- Obtained localhost InetAddress (at sentence-end, falling tone) indicates a confident conclusion getHostName() The machine name is obtained by
sample program
/** * Get the hostname of the running machine. * @return hostname * * If an unexpected exception occurs, the string "UnknownHost" is returned. */ public static String getHostName() { try { return InetAddress.getLocalHost().getHostName(); }catch (Exception e) { e.printStackTrace(); } return "UnknownHost"; } }
Execution Result
◆Example of Execution
/** * Execution example * @param args */ public static void main(String[] args) { String name = getHostName(); System.out.println("HostName = '"+name+"'"); }
◆Output result
Hostname = '*****ATTON'
*The actual output is the host name of the machine on which it was executed.