Finding IP address of a URL in Java

Prerequisite: InetAddress

getByName() : Returns the InetAddress of the given host. If the host is a literal IP address, then only its validity is checked. Fetches public IP Address of the host specified. It takes the host as an argument and returns the corresponding IP address.

Examples:

Input : www.google.com
Output : 216.58.199.164

Input : localhost
Output : 127.0.0.1

Below programs illustrate how to fetch public IP address:

Note: These programs wont run on online compilers. Use offline compilers like Netbeans, Eclipse, etc, instead.



Program 1: Fetch IP address of any url

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// how to fetch public IP Address
import java.net.*;
import java.*;
  
class GFG {
    public static void main(String args[])
        throws UnknownHostException
    {
        // The URL for which IP address needs to be fetched
        String s = "https:// www.google.com/";
  
        try {
            // Fetch IP address by getByName()
            InetAddress ip = InetAddress.getByName(new URL(s)
                                                       .getHost());
  
            // Print the IP address
            System.out.println("Public IP Address of: " + ip);
        }
        catch (MalformedURLException e) {
            // It means the URL is invalid
            System.out.println("Invalid URL");
        }
    }
}

chevron_right


Output:

Public IP Address of: www.google.com/216.58.196.164

Program 2: Fetch public IP address of the one’s system

To find public IP, use http://bot.whatismyipaddress.com. It is an online utility, to find system’s public IP. Open the URL, read a line and print the line.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate
// how to fetch public IP Address
import java.net.*;
import java.*;
  
class GFG {
    public static void main(String args[])
        throws UnknownHostException
    {
        String systemipaddress = "";
        try {
            URL url_name = new URL("http://bot.whatismyipaddress.com");
  
            BufferedReader sc = new BufferedReader(
                new InputStreamReader(url_name.openStream()));
  
            // reads system IPAddress
            systemipaddress = sc.readLine().trim();
        }
        catch (Exception e) {
            systemipaddress = "Cannot Execute Properly";
        }
        // Print IP address
        System.out.println("Public IP Address: "
                           + systemipaddress + "\n");
    }
}

chevron_right


Output:

Public IP Address: 103.62.239.242


My Personal Notes arrow_drop_up

सर्वशक्तिशाली इकलौता

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.