Home Java Java-get-example Java Get Host Name
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Java Get Host Name

Advertisement
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

Advertisement

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: October 15, 2008

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

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
mahmoud
October 21, 2011
java lesson

i don,t know what "import" is doing
Samir Arishy
December 12, 2011
Get Host Name code

It will be great , if you put a comment on how to specify a local / remote IP to get the Name. I need to get the name of a device connected to my "LOCAL" lan I have the the ip address xxx.xxx.xxx.xxx Where I can put it ? Thank you for simplifing my learning experience
Prakash
January 10, 2012
java training

Sir, I would love to get training from any of the company in this field in Delhi.
Amit Ghadge
February 6, 2012
Communication with hardware using java code

Respected... This is Amit Ghadge.... I have a query regarding hyper terminal... I want to communicate with hyper terminal using java language... So is it possible...? If yes then how to communicate with hyper terminal using java or c++ language....? will you give me some references and code of this....? Please...Also give me some ideas and references regarding this.... Thank you.... Amit Ghadge amitishere26@gmail.com
Ashen Wanniarachchi
August 30, 2013
java and databse

Dear sir, it is very useful your tutorials for my academic works.can you please send me new tutorials you are writing into my e-mail. and i would like to study java with you and become a jenious
DMCA.com