Finding IP address of a URL in Java Last Updated : 13 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Prerequisite: InetAddressgetByName() : 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 addresses: Note: These programs won't run on online compilers. Use offline compilers like Netbeans, Eclipse, etc, instead. Program 1: Fetch IP address of any URL Java // 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"); } } } Output: Public IP Address of: www.google.com/216.58.196.164 Program 2: Fetch the public IP address of one's systemTo find public IP, use http://bot.whatismyipaddress.com. It is an online utility, to find the system's public IP. Open the URL, read a line and print the line. Java // 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"); } } Output: Public IP Address: 103.62.239.242 Comment More infoAdvertise with us Next Article Finding IP address of a URL in Java B bilal-hungund Follow Improve Article Tags : Java Java-Networking Practice Tags : Java Similar Reads Java program to find IP address of your computer An IP(Internet Protocol) address is an identifier assigned to each computer and another device (e.g., router, mobile, etc) connected to a TCP/IP network that is used to locate and identify the node in communication with other nodes on the network. IP addresses are usually written and displayed in hu 2 min read Internet Address Resolution in Java Internet-Address resolution is the process of converting a domain name (like www.geeksforgeeks.org) to an IP address or vice versa. This is the first step in connecting to a network. Network devices talk to each other using IP addresses, but people tend to remember domain names better. Most of the t 4 min read Determining the IP Address & Hostname of Local Computer in Java IP Address Stands for Internet Protocol Address, is a numeric label that is assigned uniquely to each device connected within a computer network that uses the internet protocol. An IP address serves two principal functions: It identifies the host, or more specifically its network interface.It provi 3 min read java.net.URL Class in Java URL is an acronym of Uniform resource locator. It is a pointer to locate resource in www (World Wide Web). A resource can be anything from a simple text file to any other like images, file directory etc. The typical URL may look like http://www.example.com:80/index.htmlThe URL has the following part 4 min read Java Program to Determine Hostname from IP Address IP Address stands for internet protocol address. It is an identifying number that is associated with a specific computer or computer network. IP addresses are usually written and displayed in human-readable notation such as 192.168.1.35 in IPv4(32-bit IP address). When connected to the internet, the 3 min read Like