Find Website IP Address by URL in Java

In this tutorial, we will introduce how to get the ip address of a website by its url using java.

We will use InetAddress.getByName() method to implement.

import java.net.*; 
import java.*; 
  
class Cocyer{ 
    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"); 
        } 
    } 
}