Quick links: C-Tricks HR Interview Que. Code Snippets C Programs C++ Programs Java Programs PHP Examples

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.

getHostAddress()
This method returns the IP address (host address of the local system)
getHostName()
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!"); 
		} 
	} 
}

Output

IP address: 192.168.10.175
Host name : IncludeHelp_PC






COMMENTS