The Wayback Machine - https://web.archive.org/web/20240907042317/https://www.geeksforgeeks.org/java-program-find-ip-address-computer/
Open In App

Java program to find IP address of your computer

Last Updated : 09 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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 human-readable notation such as 192.168.1.35 in IPv4(32-bit IP address).

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

Prerequisites : Networking in Java | Set 1 (InetAddress class), trim() in Java.
InetAddress.getLocalHost() is used to find the private IP addresses used in LAN or any other local network.

To find public IP, we use http://bot.whatismyipaddress.com (An online utility to find your public IP), we open the URL, read a line and print the line.

Below is the Java implementation of the above steps. 

Java




// Java program to find IP address of your computer
// java.net.InetAddress class provides method to get
// IP of any host name
import java.net.*;
import java.io.*;
import java.util.*;
import java.net.InetAddress;
 
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 : " +
                      (localhost.getHostAddress()).trim());
 
        // Find public IP address
        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";
        }
        System.out.println("Public IP Address: " + systemipaddress +"\n");
    }
}


Output: 

 System IP Address : 10.0.8.204
 Public IP Address : 35.166.48.97

Note: The above output is for a machine that is used by GeeksforGeeks online compiler, ide.geeksforgeeks.org

 



Similar Reads

Why is the IP address called a "logical" address, and the MAC address is called a "physical" address?
Let us try to deeply understand, why IP address is known as "Logical" while, MAC Address is known as "Physical" address. The IP address and the MAC address are both used for addressing and identifying devices on a computer network, but they serve different purposes and operate at different levels of the network stack. An IP address is a logical add
7 min read
IP address as logical address and MAC address as Physical address
Overview :IP addresses and MAC addresses are essential for data communication. Assume there are two networks. The first network has three devices: A, B, C and the second network has three devices: X, Y, Z. If a device A from the first network wishes to send data to a device Y in the second network, it must first determine where Y is located in the
6 min read
How to Find Network IP Address of Computer Using Computer Name?
Answer: By using the command "ping" followed by the computer name in the command prompt or terminal.To find the network IP address of a computer using its computer name: Open Command Prompt or Terminal Begin by opening the Command Prompt (Windows) or Terminal (macOS/Linux) on your computer. Use the Ping Command Type "ping" followed by the computer
1 min read
MAC Address and Random MAC Address
MAC Addresses are the address which are specified by the Manufacturer.MAC address help in communicating with the devices with communicating with other devices over the network. They are 12 digit hexadecimal numbers in which first three set bits specify the manufacturer of the device and last 3 specify the specific device. Random MAC Address They ar
3 min read
Why Do We Need IP Address if We Have Already MAC Address?
Answer: The network allots an IP address to every connected device making sure that every device can be identified separately within the network. Each device is identified by its MAC address, which cannot be shared by two devices. The MAC address is assigned by the embedded Network Interface Card (NIC) of each device and cannot be altered once set.
1 min read
What is Physical Address and Logical Address in Networking?
A physical address is a unique identifier given to network interfaces for communication over a physical network segment. A logical address is a unique address assigned to each networked device to identify its location and enable routing. In this article, we will discuss Physical Address and Logical Address. What is a Physical Address?The physical a
5 min read
Difference Between MAC Address and IP Address
Both MAC Address and IP Address are used to uniquely define a device on the internet. NIC Card’s Manufacturer provides the MAC Address, on the other hand, the Internet Service Provider provides the IP Address. In this article, we will learn about MAC Addresses, IP Addresses, and their differences. What is MAC Address?MAC, or Media Access Control is
6 min read
Difference Between Network Address Translation (NAT) and Port Address Translation (PAT)
In the world of networking, most of the uses of IP addresses are important due to the limited availability of IPv4 addresses which is a part of network address translation. Two technologies that have been developed to address this problem are Network Address Translation and Port Address Translation. These technologies not only help to keep IP addre
6 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 provides the location of the host in the network, and th
3 min read
How to Find Your Mac Address?
Prerequisite : Introduction of MAC Address In this article we will learn How to Find Our Mac Address. 1. Finding mac address in Windows : Click Start or click in the search box and type cmd. Click on the Command Prompt.In cmd window type ipconfig /all and press enter. All the adapters are listed and each have physical address known as mac address.
1 min read
How to Find Your IP Address?
A computer network uses two types of IP addresses: 1. Public IP / External IP: A Public IP is an IP address that your residence or company router receives from your ISP. It is an IP address that is used to communicate outside the local network. 2. Private IP / Internal IP: A Private IP address is useful for connecting devices within that same netwo
2 min read
How Many Ways to Find Your IP Address?
Answer: You can find IP address by a command like `ipconfig`or `ifconfig` in the terminal.Finding your IP address can be done easily using various methods, depending on the device and operating system you're using. For WindowsPress Win + R to open the Run dialog box.Type "cmd" and press Enter to open the Command Prompt.In the Command Prompt, type "
2 min read
How do I Find IP Address of a Device Connected to My Computer?
Answer: On Windows, use the command `arp -a` in Command Prompt & on macOS or Linux, use `arp -a` or `ip neigh show` in the Terminal.To find the IP address of a device connected to your computer, use network utility commands accessible through the command line interface of your operating system. This allows for the identification of devices with
1 min read
How to Hide Your IP Address?
Hiding an IP address means borrowing a different IP address to mask the original one and make the identity private. We can borrow different IP addresses and stay hidden. There are several reasons behind hiding IP Addresses. A few of them are given below: To bypass any filters, bans, checklistsHide the geographical identityTo prevent any digital foo
5 min read
How to Finding a Specific Computer from an IP address on a Network?
Answer: Use the command `ping -a IPaddress` to find the hostname associated with a specific IP address on a network.Open Command Prompt On a Windows machine, open Command Prompt by searching for it in the Start menu or by pressing `Windows + R`, typing `cmd`, and pressing Enter. Use Ping CommandType `ping -a IP_address`, replacing `IP_address` with
1 min read
Which Command is Used to View the IP Address and Default Gateway of a Computer?
Answer: The command used to view the IP address and default gateway of a PC is `ipconfig` on Windows and `ip route` or `ifconfig` on Linux/Unix/MacOS systems.To view the IP address and default gateway of a PC, the command differs by operating system: WindowsUse `ipconfig` in Command Prompt. It displays network interfaces. Find the `IPv4 Address` fo
1 min read
Who Assigns an IP Address to the Computer?
Answer: An IP address is assigned to a computer by a Dynamic Host Configuration Protocol (DHCP) server, or manually by an administrator in static configurations.Dynamic Host Configuration Protocol (DHCP)Most commonly, a DHCP server automatically assigns an IP address to a computer within a network. The DHCP server, which can be a router or a dedica
1 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 IP address allows the computers to send and recei
3 min read
C Program to find IP Address, Subnet Mask & Default Gateway
Terminology IP Address : An IP address, short for Internet Protocol address, is an identifying number for a piece of network hardware. Having an IP address allows a device to communicate with other devices over an IP-based network like the internet. Subnet mask: A subnet mask is a 32-bit number used to differentiate the network component of an IP a
2 min read
Difference between Micro Computer and Mini Computer
1. Micro Computer :Micro Computer, as the name suggests, is a personal computer that is specially designed for personal use and generally consists of a single chip that is CPU, data memory, I/O buses, etc. It can be used by one person at a time. Its type includes tablet and smartphone microcomputers, desktop microcomputers, workstations, and server
6 min read
How To Connect One Computer To Another Computer via LAN?
Answer: To connect one computer to another computer via LAN, use an Ethernet cable to directly connect their Ethernet ports.The most common methods include: Wired Ethernet Connection: Use an Ethernet cable to directly connect the two computers' network ports. For more than two computers, connect each computer to a network switch or router with Ethe
1 min read
C Program to display hostname and IP address
Method 1: There are many ways to find Hostname and IP address of a local machine. Here is a simple method to find hostname and IP address using C program. We will be using the following functions :- gethostname() : The gethostname function retrieves the standard host name for the local computer. gethostbyname() : The gethostbyname function retrieve
3 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 time, an Internet address is resolved by a Domain N
4 min read
How to validate an IP address using Regular Expressions in Java
Given an IP address, the task is to validate this IP address with the help of Regular Expressions.The IP address is a string in the form "A.B.C.D", where the value of A, B, C, and D may range from 0 to 255. Leading zeros are allowed. The length of A, B, C, or D can't be greater than 3.Examples: Input: str = "000.12.12.034" Output: True Input: str =
3 min read
Pinging an IP address in Java | Set 1
PING stands for Packet InterNet Groper in computer networking field. It's a computer network administration software used to test the reachability of a host on an Internet Protocol (IP) network. It measures the round-trip time for messages sent from the originating host to a destination computer that are echoed back to the source. Ping operates by
2 min read
Pinging an IP address in Java | Set 2 (By creating sub-process)
In article Pinging an IP address in Java, we have discussed how to ping an IP address using java.net.InetAddress.isReachable() method. In this post we will discuss how to execute ping command by creating a sub-process. Prerequisite : ProcessBuilder class , Process classBelow Java program creates a method commands() which take list of command(ping)
3 min read
Check if Email Address is Valid or not in Java
Given a string, find if the given string is a valid email or not. Input : email = "review-team@geeksforgeeks.org" Output : Yes Input : email = "contribute@geeksforgeeks..org" Output : No Explanation : There is an extra dot(.) before org. Prerequisite: Regular Expressions in Java Regular Expressions Regular Expressions or Regex is an API for definin
2 min read
Finding IP address of a URL in Java
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 : localho
2 min read
Alternate method to find DBA from given IP address
One more drawback in the previous method finding DBA from IP address is that if we have to find only DBA of the network and not Net Id of the network but by using that method DBA of the network can't be obtained without finding Net Id of the network. In this approach we will use shortcut approach for finding the both NetId and DBA. Using this appro
2 min read
How to Find a Loopback Address?
In a Computer Network, the Loopback Address is an important address that is used for testing and troubleshooting the network connectivity on the local machine. this address provides a path for a computer system to transfer network packets to itself without involving any external networks. Loopback address allows developers and system administrators
4 min read
Article Tags :
Practice Tags :