{"id":981,"date":"2018-09-17T19:01:45","date_gmt":"2018-09-17T10:01:45","guid":{"rendered":"https:\/\/chat-messenger.com\/?p=981"},"modified":"2018-12-23T01:50:32","modified_gmt":"2018-12-22T16:50:32","slug":"inetaddress-getlocalhost","status":"publish","type":"post","link":"https:\/\/chat-messenger.com\/en\/blog\/java\/inetaddress-getlocalhost","title":{"rendered":"Get the hostname of the machine running Java"},"content":{"rendered":"<h2 class=\"common_title\"><a name=\"0\">Get the hostname of the machine running Java<\/a><br \/><strong>InetAddress.getLocalHost().getHostName()<\/strong><\/h2>\n<p>\nof the machine running in Java.<strong>host name<\/strong>(in Japanese history)<strong>Machine name<\/strong>The following is a sample program to obtain the<br \/>\nThe sample program uses the following method to obtain the host name.\n<\/p>\n<div class=\"point\">\n<ul>\n<li><strong>InetAddress.getLocalHost()<\/strong> Represents the local host by <strong>InetAddress<\/strong> Obtains an instance of<\/li>\n<li>Obtained localhost <strong>InetAddress<\/strong> (at sentence-end, falling tone) indicates a confident conclusion <strong>getHostName()<\/strong> The machine name is obtained by<\/li>\n<\/ul>\n<\/div>\n<h2 class=\"common_title\"><a name=\"1\">sample program<\/a><\/h2>\n<pre class=\"sample_src\">\/**\r\n * Get the hostname of the running machine.\r\n * @return hostname\r\n * * If an unexpected exception occurs, the string \"UnknownHost\" is returned.\r\n *\/\r\npublic static String getHostName() {\r\n    try {\r\n        return InetAddress.getLocalHost().getHostName();\r\n    }catch (Exception e) {\r\n        e.printStackTrace();\r\n    }\r\n    return \"UnknownHost\"; }\r\n}\r\n<\/pre>\n<p><script type=\"text\/javascript\" src=\"https:\/\/chat-messenger.com\/js\/common.js?dd12sssas2223\" charset=\"UTF-8\"><\/script><br \/>\n<script type=\"text\/javascript\"> writePR(); <\/script><\/p>\n<h2 class=\"common_title\"><a name=\"2\">Execution Result<\/a><\/h2>\n<p>\u25c6Example of Execution<\/p>\n<pre class=\"sample_src\">\/**\r\n * Execution example\r\n * @param args\r\n *\/\r\npublic static void main(String[] args) {\r\n    String name = getHostName();\r\n    System.out.println(\"HostName = '\"+name+\"'\");\r\n}\r\n<\/pre>\n<p>\u25c6Output result<\/p>\n<pre class=\"console\">Hostname = '*****ATTON'\r\n<\/pre>\n<p>*The actual output is the host name of the machine on which it was executed.<\/p>","protected":false},"excerpt":{"rendered":"<p>Get the host name of the machine running Java InetAddress.getLocalHost().getHos [\u2026]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"swell_btn_cv_data":""},"categories":[19],"tags":[],"_links":{"self":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/981"}],"collection":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/comments?post=981"}],"version-history":[{"count":4,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/981\/revisions"}],"predecessor-version":[{"id":1870,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/posts\/981\/revisions\/1870"}],"wp:attachment":[{"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/media?parent=981"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/categories?post=981"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/chat-messenger.com\/en\/wp-json\/wp\/v2\/tags?post=981"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}