{"id":2449,"date":"2017-06-17T06:35:00","date_gmt":"2017-06-17T06:35:00","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2449"},"modified":"2021-03-21T06:38:02","modified_gmt":"2021-03-21T06:38:02","slug":"write-a-java-program-to-print-the-machine-ip-address","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/write-a-java-program-to-print-the-machine-ip-address\/","title":{"rendered":"Write a Java program to print the machine IP address"},"content":{"rendered":"<h3>Introduction<\/h3>\n<p>Write a simple Java program to output machine IP address and hostname to the console. InetAddress class is used. import java.net.InetAddress and wrap the code in a try-catch block.<\/p>\n<p>The InetAddress java class provides methods to resolve hostnames to<br \/>\ntheir IP addresses and vice versa. Below is a sample Java program to print<br \/>\nhostname and IP Address.<\/p>\n<h3>Java Program<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.net.InetAddress;\r\nimport java.net.UnknownHostException;\r\n\r\n\/********************************************\r\n\/\/  IPAddressDemo.java\r\n\/\/\r\n\/\/  www.TestingDocs.com\r\n********************************************\/\r\n\r\npublic class IPAddressDemo {\r\n  public static void main(String[] args) {\r\n    \/\/try-catch block\r\n    try {\r\n      InetAddress hostMachine = InetAddress.getByName(\"localhost\");\r\n      System.out.println(hostMachine.getHostName()); \/\/Print hostname\r\n      System.out.println(hostMachine.getHostAddress());\/\/Print ipaddress\r\n    }catch(UnknownHostException uhe) {\r\n      uhe.printStackTrace();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3>Command Prompt<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2498\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Command-Prompt-Hostname-Windows-10.png\" alt=\"Hostname Command Prompt\" width=\"1731\" height=\"1026\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Command-Prompt-Hostname-Windows-10.png 1731w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Command-Prompt-Hostname-Windows-10-300x178.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Command-Prompt-Hostname-Windows-10-1024x607.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Command-Prompt-Hostname-Windows-10-768x455.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Command-Prompt-Hostname-Windows-10-1536x910.png 1536w\" sizes=\"auto, (max-width: 1731px) 100vw, 1731px\" \/><\/p>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2499\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Machine-IP-Address-Java-Program.png\" alt=\"Machine IP Address Java Program\" width=\"1735\" height=\"975\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Machine-IP-Address-Java-Program.png 1735w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Machine-IP-Address-Java-Program-300x169.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Machine-IP-Address-Java-Program-1024x575.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Machine-IP-Address-Java-Program-768x432.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Machine-IP-Address-Java-Program-1536x863.png 1536w\" sizes=\"auto, (max-width: 1735px) 100vw, 1735px\" \/><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2449\" data-postid=\"2449\" class=\"themify_builder_content themify_builder_content-2449 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Introduction Write a simple Java program to output machine IP address and hostname to the console. InetAddress class is used. import java.net.InetAddress and wrap the code in a try-catch block. The InetAddress java class provides methods to resolve hostnames to their IP addresses and vice versa. Below is a sample Java program to print hostname [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2449","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2449"}],"version-history":[{"count":3,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2449\/revisions"}],"predecessor-version":[{"id":19736,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2449\/revisions\/19736"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}