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

Comments and Discussions!

Copyright © 2023 www.includehelp.com. All rights reserved.