Getting the host name in Java
In this tutorial, we will see how to get the IP address and host name using Java API.
InetAddress
Get the host name
in Java using
The package java.net contains classes for handling the IP address and host name of the current machine InetAddress
.
getLocalHost()
By collecting the system's IP address
using the function of the same class , we can get the system name of the Windows computer using InetAddress
the class in the java.net package getHostName()
.
If the Security Manager is not authorized to obtain the address of the local host, getHostName()
a textual representation of the IP address is provided.
InetAddress
getLocalHost()
The and methods of the class getByName (String host)
handle local and remote hosts, respectively.
grammar:
InetAddress inetadd = InetAddress.getLocalHost();
This function will inetadd
create an instance of the local server for running the Java program InetAddress
. As a result, the address of the local host is returned.
InetAddress
This is done
by retrieving the host name from the system and converting it to .
InetAddress
Demonstrates how to use the class to get the host name
in Java
First, import the following libraries.
java.net.InetAddress
We will show you how to retrieve the host name in Java through a simple example.
InetAddress inetadd = InetAddress.getLocalHost();
We will use the method to create an instance of for a local server InetAddress
named that is run by a Java program .inetadd
InetAddress
String name = inetadd.getHostName();
We will use getHostName()
the method to get the host name and save it in name
a string type variable called .
String address = inetadd.getHostAddress();
After that, with the help of getHostAddress()
the method, we will get the host address and save it in address
a string type variable called . This will throw if it cannot resolve the local or site hostname to an address UnknownHostException
.
source code:
import java.net.InetAddress;
import java.net.UnknownHostException;
public class HostnameExample {
public static void main(String[] args) {
try {
InetAddress inetadd = InetAddress.getLocalHost();
String name = inetadd.getHostName();
String address = inetadd.getHostAddress();
System.out.println("HostName is : " + name);
System.out.println("Host Address is: " + address);
} catch (UnknownHostException u) {
}
}
}
Output:
$javac HostnameExample.java
$java -Xmx128M -Xms16M HostnameExample
HostName is : f97aedb6192a
Host Address is: 127.0.0.2
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
Get the IP address of the current device in Java
Publish Date:2025/04/14 Views:53 Category:Java
-
An Internet Protocol (IP) address is an identifier for each device connected to a TCP/IP network. This identifier is used to identify and locate nodes in the middle of communication. The IP address format, such as 127.0.0.0, is a human-read
Generate a random double value between 0 and 1 in Java
Publish Date:2025/04/14 Views:153 Category:Java
-
This article will introduce three methods to generate double random values between 0 and 1 of primitive types. To demonstrate the randomness of the generated values, we will use a loop to generate ten random double values betwee
Setting the seed of a random generator in Java
Publish Date:2025/04/14 Views:62 Category:Java
-
A seed is a number or vector assigned to a pseudo-random generator to generate the desired sequence of random values. If we pass the same seed, it will generate the same sequence. We usually assign the seed as the system time. In this way,
Switching on enumeration types in Java
Publish Date:2025/04/14 Views:96 Category:Java
-
This article explains how to use enum in Java . We will use statement switch with enum in two ways . switch In Java, we use traditional switch and case to perform enumeration operations. switch In this example, we SwitchEnum create an enume
How to delay for a few seconds in Java
Publish Date:2025/04/14 Views:131 Category:Java
-
This tutorial explains how to create program delays in Java and gives some sample code to understand it. There are several ways to create a time delay, such as TimeUnit.sleep() ,, ScheduleAtFixedRate() etc. Thread.sleep() Let's look at an e
How to Convert Hashmap to JSON Object in Java
Publish Date:2025/04/14 Views:112 Category:Java
-
This article explains how to convert a Hashmap to a JSON object in Java. We will see a detailed example of creating a hashmap and then converting it to a JSON object. Hashmap and JSON are both very commonly used by developers as they help u
How to sort a Map by value in Java
Publish Date:2025/04/14 Views:171 Category:Java
-
This tutorial explains how to Mapkey, value sort pairs by value in Java and lists some sample codes to understand it. There are several ways to Map sort . Here we use sort() the , sorted() method, and Comparator interface. Let's look at an
How to Print a HashMap in Java
Publish Date:2025/04/14 Views:85 Category:Java
-
This tutorial explains how to print HashMap an element in Java and also provides some sample code to understand the topic. HashMap It is Map an implementation class of the interface and is used to collect elements into key and value pairs.
Updating Hashmap Values in Java
Publish Date:2025/04/14 Views:88 Category:Java
-
This article explains how to use the two methods contained in the HashMap class in Java - update put() and replace() update the values in a HashMap. hashmap.put() Update the value in Hashmap in Java using When we want to HashMap inser