How to find the IP address of the local host in Java
Java Network API provides a way to find the IP address of local host from Java program using java.net. InetAddress
class. It is rare that you need the IP address of local host in Java program. Most of the time, I use Unix commands to find the IP address of local host. For all practical purposes where program does not require IP address but we need to solve any network problem, use DOS or Windows commands or use Linux commands. Recently one of my friend faced this question in core Java interview, they expect Java developers to have some experience in socket programming, but it is difficult to answer this fact based question until you know or you have done it, which prompted us to write this article.
In this Java tutorial, we will see how to find the IP address of the local host from within a Java program. By the way, it is always a good idea to remember the list of Unix network commands to troubleshoot any network issues related to Java applications in Unix environment.
The IP address of the local host from the Java program
As I said, the in the java.net package InetAddress
is used to represent IP addresses in Java. An IP address is a 32-bit or 128-bit unsigned number used by the IP protocol, which is the backbone of many popular protocols such as TCP and UDP.
There are two types of IP addresses, IPv4 and IPv6. An IP address is associated with a host, and the host can be found through the host name resolution process.
Hostname resolution is performed by combining the local machine configuration and network naming services such as DNS (Domain Name System) and NIS (Network Information Service). InetAddress
There is a method to resolve hostname to IP address and vice versa. Here is the complete code example to find IP address from Java program.
import java.net.UnknownHostException;
/**
* 用于查找本地主机 IP 地址的简单 Java 程序。 这个程序使用
* InetAddress 从 java.net 包中查找 IP 地址。
*
*/
public class IPTest {
public static void main(String args[]) throws UnknownHostException {
InetAddress addr = InetAddress.getLocalHost();
// 获取本地主机的 IP 地址 - getHostAddress 以文本格式返回 IP 地址
String ipAddress = addr.getHostAddress();
System.out.println("IP address of localhost from Java Program: " + ipAddress);
//Hostname
String hostname = addr.getHostName();
System.out.println("Name of hostname : " + hostname);
}
}
The output of the above code is as follows
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Store Div Id in PHP variable and pass it to JavaScript
Publish Date:2025/04/13 Views:51 Category:PHP
-
This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s
Use of Linux command at - set time to execute command only once
Publish Date:2025/04/08 Views:158 Category:OPERATING SYSTEM
-
This article mainly involves a knowledge point, which is the atd service. Similar to this service is the crond service. The functions of these two services can be similar to the two functional functions of javascript. Those who have learned
How to Find IP Address from Host Name in Windows Linux and Unix
Publish Date:2025/04/06 Views:89 Category:OPERATING SYSTEM
-
How many times in a day do we have a hostname and want to know the IP address? While dealing with network commands in Unix, hostname to IP address and IP address to hostname conversion is one of the common things we need to do for many thin
Obtaining an IPv4 Address on Unix and Linux
Publish Date:2025/04/05 Views:200 Category:OPERATING SYSTEM
-
IP stands for Internet Protocol, which specifies the principles of Internet communications. An Internet Protocol (IP) address is a unique identifier for each device on the Internet, allowing data to be transferred between connected devices.
Get the IP address of the Docker container
Publish Date:2025/03/25 Views:103 Category:Docker
-
This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con
How to get IP address in CentOS
Publish Date:2025/03/23 Views:109 Category:OPERATING SYSTEM
-
This short article is a brief introduction to CentOS followed by a brief discussion on how we can get the server IP address in CentOS using the Command Line Interface (CLI). This article will discuss some of the commands and their usage for
Get the Primary IP Address in Linux
Publish Date:2025/03/22 Views:70 Category:OPERATING SYSTEM
-
There are various ways to get network details in Linux. We will learn some of them in this article. This simple guide is all about using different commands that can be used to get the primary IP address in Linux operating system using Bash
Design Patterns in Java - Visitor Pattern
Publish Date:2025/03/19 Views:175 Category:ALGORITHM
-
Today, we are going to learn one of the most useful patterns, the Visitor Pattern. What is the Visitor pattern? Well, let's look at an example. Let's say you're a software engineer working at a university. The university rarely has establis
How to use Strategy Pattern in Java?
Publish Date:2025/03/19 Views:142 Category:ALGORITHM
-
Hi everyone, you might have heard, “Can you tell me about any design pattern other than Singleton design pattern that you have used recently in your project?”. This is one of the popular questions in various Java interviews in recent ye