To retrieve network interface information using the NetworkInterface
class in Java, you can use the java.net
package which provides the NetworkInterface
class. This class allows you to get information about network interfaces such as IP addresses, display names, names, hardware (MAC) addresses, and more.
Here is an example illustrating how to retrieve information about all network interfaces available on the machine:
Code Example
package org.kodejava.net;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
public class NetworkInterfaceInfo {
public static void main(String[] args) {
try {
// Get all network interfaces
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterface networkInterface = networkInterfaces.nextElement();
// Display interface name and display name
System.out.println("Interface Name: " + networkInterface.getName());
System.out.println("Display Name: " + networkInterface.getDisplayName());
// Display MAC address
byte[] mac = networkInterface.getHardwareAddress();
if (mac != null) {
StringBuilder macAddress = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
macAddress.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
}
System.out.println("MAC Address: " + macAddress.toString());
} else {
System.out.println("MAC Address: Not available");
}
// Display the IP addresses associated with this interface
Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
while (inetAddresses.hasMoreElements()) {
InetAddress inetAddress = inetAddresses.nextElement();
System.out.println("IP Address: " + inetAddress.getHostAddress());
}
// Display other information
System.out.println("MTU: " + networkInterface.getMTU());
System.out.println("Is Loopback: " + networkInterface.isLoopback());
System.out.println("Is Up: " + networkInterface.isUp());
System.out.println("Is Virtual: " + networkInterface.isVirtual());
System.out.println("Supports Multicast: " + networkInterface.supportsMulticast());
System.out.println("--------------------------------------------");
}
} catch (SocketException e) {
e.printStackTrace();
}
}
}
Explanation:
- Getting All Network Interfaces:
TheNetworkInterface.getNetworkInterfaces()
method retrieves an enumeration of all network interfaces available on the machine. -
Retrieving Basic Information:
getName()
: Returns the name of the network interface.getDisplayName()
: Returns a user-readable name for the network interface.
- Retrieving the MAC Address:
getHardwareAddress()
: Returns the hardware (MAC) address of the network interface as an array of bytes.
- Retrieving IP Addresses:
getInetAddresses()
: Returns all assigned IP addresses (both IPv4 and IPv6) for this interface.
- Flags and Features of the Interface:
isUp
: Checks if the network interface is up and operational.isLoopback
: Checks if the interface is a loopback interface.isVirtual
: Checks if it is a virtual interface.supportsMulticast
: Indicates whether the interface supports multicasting.getMTU
: Returns the Maximum Transmission Unit (MTU) of the interface.
- Handling SocketException:
Accessing network interfaces and their properties might throw aSocketException
, so wrap the logic in a try-catch block.
Output Example (Depending on Your Machine’s Network Setup)
Interface Name: ethernet_32768
Display Name: Intel(R) Ethernet Connection (18) I219-LM
MAC Address: C4-EF-BB-80-01-D0
IP Address: fe80:0:0:0:8e43:dfa1:eb66:b106%ethernet_32768
MTU: 1500
Is Loopback: false
Is Up: false
Is Virtual: false
Supports Multicast: true
--------------------------------------------
Interface Name: loopback_0
Display Name: Software Loopback Interface 1
MAC Address: Not available
IP Address: 0:0:0:0:0:0:0:1
IP Address: 127.0.0.1
MTU: 1500
Is Loopback: true
Is Up: true
Is Virtual: false
Supports Multicast: true
--------------------------------------------
This code allows you to inspect all network interfaces, their properties, and associated information in Java.
Latest posts by Wayan (see all)
- How do I handle file uploads using Jakarta Servlet 6.0+? - April 23, 2025
- How do I serve static files through a Jakarta Servlet? - April 23, 2025
- How to Use Java 17 Text Blocks for Multiline Strings - April 22, 2025