Java program to find IP address of your computer

An IP(Internet Protocol) address is an identifier assigned to each computer and other 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 human-readable notation such as in IPv4(32-bit IP address).

An IP address serves two principle functions : host or network interface identification and local addressing. It’s role has been characterized as follows : “A name indicate what we seek. An address indicate where it is. A route indicate how to get there.”

Prerequisites : Networking in Java | Set 1 (InetAddress class), trim() in Java.

InetAddress.getLocalHost() is used to find private IP address used in LAN or any other local network.

To find public IP, we use (An online utility to find your public IP), we open the URL, read a line and print the line.

Below is Java implementation of above steps.





// Java program to find IP address of your computer
// class provides method to get
// IP of any host name
import java.util.*;
public class JavaProgram
    public static void main(String args[]) throws Exception
        // Returns the instance of InetAddress containing
        // local host name and address
        InetAddress localhost = InetAddress.getLocalHost();
        System.out.println("System IP Address : " +
        // Find public IP address
        String systemipaddress = "";
            URL url_name = new URL("");
            BufferedReader sc =
            new BufferedReader(new InputStreamReader(url_name.openStream()));
            // reads system IPAddress
            systemipaddress = sc.readLine().trim();
        catch (Exception e)
            systemipaddress = "Cannot Execute Properly";
        System.out.println("Public IP Address: " + systemipaddress +"\n");



 System IP Address :
 Public IP Address :

Note : The above output is for machine that is used by GeeksforGeeks online compiler,

This article is contributed by Pramod Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :


Please write to us at to report any issue with the above content.