How to get IP address in Java using InetAddress

An Internet Protocol address (IP address) is a numerical label assigned to each device (e.g., computer, printer) participating in a computer network that uses the Internet Protocol for communication. The designers of the Internet Protocol defined an IPv4 address as a 32-bit number.

In this tutorial we are going to see how can you get the IP Address that is assigned to your own machine inside your local network and the IP Addresses assigned to specific Domain Names(e.g.…).

To do that we are going to use InetAddress.To be more specific we are going to use:

  • getLocalHost().getHostAddress() method of InetAddress to get the IP Address of our machine in our local network
  • getByName() method of InetAddress to get the IP Address of a specific Domain Name
  • getAllByName() method of InetAddress to get all the IP Address of a specific Domain Name.

So, let’s see the code:



public class GetIpAddress {

	public static void main(String[] args) throws UnknownHostException {

		// print the IP Address of your machine (inside your local network)

		// print the IP Address of a web site

		// print all the IP Addresses that are assigned to a certain domain
		InetAddress[] inetAddresses = InetAddress.getAllByName("");

		for (InetAddress ipAddress : inetAddresses) {


This was an example of how to get IP address in Java using InetAddress.

Ilias Tsagklis

Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Newest Most Voted
Inline Feedbacks
View all comments
5 years ago

how to checks this in CMD?

Back to top button