Java Program to Get the IP address

Write a Java program to get the IP address with an example. In Java, InetAddress class has all the necessary Internet Protocol (IP) address information. So, we used the getLocalHost method to get the localhost address and the getHostAddress method to get the IP address.

package NumPrograms;

import java.net.InetAddress;

public class IpAddress1 {

	public static void main(String[] args) throws Exception {
		
		InetAddress myIpAdd = InetAddress.getLocalHost();
		
		System.out.println("My IP Address is");
		System.out.println(myIpAdd.getHostAddress());
	}

}
Java Program to Get the IP address