Get IP address in Java

In this tutorial, we learn about How to get an  IP address in Java.

Introduction :

Getting an IP address in Java means finding out the unique number that identifies a computer on a network. This number, called an IP address, helps devices find and talk to each other online. Java, a popular programming language, has tools that make it easy. Using simple code, you can quickly find out the IP address of your computer or any other computer on your network. This is useful for networking tasks, like connecting to the internet or communicating between devices.

Get IP address in Java:

the key points to understand :

  1. IP Address: An IP address is a unique number that identifies a computer on a network.
  1. Java Class: Use the InetAddress' class in Java to work with IP addresses.
  2. Import Statement: Include ‘import java.net.*;' at the beginning of your Java code to use networking features.
  3. Get Local Host: Use InetAddress.getLocalHost()' to get the IP address of your computer.
  4. Get Host Address: Call the getHostAddress()' method on the InetAddress' object to get the IP address as a string.

Example code :

import java.net.*;

public class GetIPAddress {
    public static void main(String[] args) {
        try {
            InetAddress ip = InetAddress.getLocalHost();
            System.out.println("IP Address: " + ip.getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }

Code explanation :

This Java code uses the `InetAddress` class , which is the computer running the code. It imports the `java.net` package for networking functionalities. Inside the `main` method, it tries to get the local host’s IP address using `InetAddress.getLocalHost()`, which returns an `InetAddress` object representing the local host. Then, it prints out the IP address of the local host using `ip.getHostAddress()`. If any error occurs, like if the IP address can’t be found, it catches and prints the exception using `e.printStackTrace()`. Overall, this code retrieves and displays the IP address of the computer running the Java program.

Output:

The output of the given Java code depends on the specific IP address of the computer on which it is run. When you run this program on your computer, it will display the IP address of your local machine.Here is an example of what the output might look like:

This image is belongs  to the output screen of getting an  IP address in Java

IP Address: 192.168.1.2

Related links :

‘InetAddress’

import java.net.*;

InetAddress.getLocalHost()’

Conclusion :

In conclusion, obtaining the IP address of a computer in Java is straightforward using the ‘InetAddress' class. By calling ‘getLocalHost()' and ‘getHostAddress()' methods, one can easily retrieve and display the IP address. This functionality proves invaluable for various networking tasks, providing essential information for communication between devices on a network.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top