Home Java Java-get-example Java Get Host Name



Java Get Host Name
Posted on: October 15, 2008 at 12:00 AM
In this Example you will learn how to get host name in Java. Go through the below given code to understand how it works and gets the host name of a computer or an IP address using Java application language.

Java Get Host Name

     

In this Example you will learn how to get host name in Java. Go through the below given code to understand how it works and gets the host name of a computer or an IP address using Java application language.

The given program will first check Java's Security Manager to ensure if the hostname search is permitted or not. Although, it is allowed on most standard environments by default.

 

Java code to get host name

import java.net.*;
import java.io.*;

public class GetHostName{
 public static void main(String [] args) {
 try {
  InetAddress addr = InetAddress.getLocalHost();
  byte[] ipAddr = addr.getAddress();
  String hostname = addr.getHostName();
  System.out.println("hostname="+hostname);
  catch (UnknownHostException e) {
  }

  }
}

Output will be displayed as:

Download Source Code

Related Tags for Java Get Host Name:
javaccomapplicationlanguageiogetipnameusingthisaddressappcomputerexampleaddworktohostlearnexamlaneareilitliputinstamoscaddosjadesthroughcomputeagemehowhrppcatxaxampsessatkisllivmplgogetseaandarcodcodevassthavbelostatiapicaicapleplndodeonomo


More Tutorials from this section

Ask Questions?    Discuss: Java Get Host Name   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.