Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Network Find Your Host Name

Related Tutorials


 
 

Share on Google+Share on Google+

Find Your Host Name

Advertisement
Here we are going to explore a method to retrieve the host name of the local system in a very simple example

Find Your Host Name

     

Here we are going to explore a method to retrieve the host name of the local system in a very simple example. Here we are just call the InetAddress class and after made a object of that class we call a getLocalHost() method and pass it in the object.

In this way we find out the ip address of the local system in a very simple manner. the complete code of the example is as under.

Here is the Code of the Example :

Hostname.java

import java.net.*;

public class Hostname{
  public static void main(String args[]){
  try{
  InetAddress local= InetAddress.getLocalHost();
  System.out.println ("Local hostname is: " + local.getHostName());
  }
  catch (UnknownHostException e){
  System.err.println ("Can't detect localhost : " + e);
  }
  }
}

The above example shows the host name of the local system. The output of the above example is as under, where the local hostname shows as comp20.

Here is the Output of the Example :

C:\roseindia>javac Hostname.java

C:\roseindia>java Hostname
Local hostname is: comp20

Download of this example.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 16, 2009

Related Tutorials

Discuss: Find Your Host Name  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com