How to Get IP Address of the Current Device in Java
The Internet Protocol (IP) address can be an identifier for each device connected to a TCP/IP network. This identifier is used to identify and locate the nodes in communication in-between.
The IP address format, such as 127.0.0.0, is a human-readable notation. This tutorial demonstrates how to get the IP address of the current machine using Java.
Get the System IP Address of the Current Device in Java
IP addresses have two main functions: local addressing and host or network interface identification. Let us try to get the system IP address of our current device in Java.
Example:
package Delfstack;
import java.net.InetAddress;
import java.net.UnknownHostException;
public class Get_IPAddress {
public static void main(String[] args) {
InetAddress My_IP;
try {
My_IP = InetAddress.getLocalHost();
System.out.println("The IP address of the Current Device is: " + My_IP.getHostAddress());
} catch (UnknownHostException e) {
e.printStackTrace();
}
}
}
Output:
The IP address of the Current Device is: 172.23.96.1
After running the code, we get the system IP address of the current device from which the Java code compiles.
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook