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 print the hostname and IP address of Local system

Submitted by BalRam Dixit, on May 19, 2017
Java program to find and print the host name, Ip address of local system. This program will print the IP address and Hostname of the system on which program will be executed.

In this java program, we will learn to get the IP address and Hostname of Local System using some of the methods of java.net.* package.

This method returns the IP address (host address of the local system)
This method returns the host name (computer name of the local system)

Consider the program

import java.net.*; 
public class LocalIp 
	public static void main(String[] args) { 
		try { 
			InetAddress address = InetAddress.getLocalHost();
			System.out.println("IP address: " + address.getHostAddress());
			System.out.println("Host name : " + address.getHostName());  
		catch (UnknownHostException uhEx) { 
			System.out.println( "Could not find local address!"); 


IP address:
Host name : IncludeHelp_PC

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