C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programs

Java program to get the IP address, Hostname based on given Hostname

Submitted by BalRam Dixit, on May 19, 2017
Learn: How to get the host ip address (website ip address), host name based on given host name (website name) in java, this program will take a host name as input and print the host name, ip address.

In this java program, we will learn how to get the IP Address and Host name, when host name is given. This program will read host name (website name) as an input and based on this input program will find and print the IP address and host name.

Consider the program

import java.net.*; 
import java.util.*; 

public class IPDemo 
	public static void main(String[] args){ 
		String host; 
		Scanner input = new Scanner(System.in); 
		System.out.print("\n Enter host name: "); 
		host = input.nextLine(); 
		try { 
			InetAddress address = InetAddress.getByName(host);
			System.out.println("IP address: " + address.getHostAddress());
			System.out.println("Host name : " + address.getHostName());  
			System.out.println("Host name and IP address: " + address.toString()); 

		catch (UnknownHostException ex) {
		     System.out.println("Could not find " + host); 


Enter host name: www.google.com
IP address:
Host name : www.google.com
Host name and IP address: www.google.com/

You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions