How to Retrieve Information About a Network Interface in Java
In this tutorial, we are going to retrieve information about a network interface in Java. A Network Interface Card, or NIC, is a type of network interface. It is a piece of hardware—or, more accurately, a software interface—that makes it possible for a system to connect to a computer network. It makes it possible for the device—a computer, router, etc.—to speak with other devices connected to the same or separate networks. Java allows us to access information about network interfaces through the java.net.NetworkInterface class. This class offers ways to find out more about the network interfaces that the system has available. A network interface card (NIC) is primarily used to enable communication between the device it is installed in—a computer, router, or other network-capable device—and other devices that are either connected to the same network or separate networks. A NIC may be a stand-alone card that slots into an expansion slot or it may be an incorporated part of the motherboard. It is capable of supporting both wireless and wired connections, such as Ethernet and Wi-Fi. Each network interface card (NIC) is assigned a distinct hardware address, or MAC (Media Access Control) address, that aids in network identification and management.
Implementation to Retrieve Information about a Network Interface in Java
Filename: NetworkInterfaceInformation.java
import static java.lang.System.out; import java.io.*; import java.net.*; import java.net.NetworkInterface; import java.util.*; public class NetworkInterfaceInformation { public static void main(String args[]) throws SocketException { // Compile the network interfaces. Enumerationnetworks = NetworkInterface.getNetworkInterfaces(); // Repeatedly running through the list of network interfaces for (NetworkInterface netInf : Collections.list(networks)) { out.printf("Display name: %s ", netInf.getDisplayName()); out.printf("Name: %s ", netInf.getName()); // Display Sub Interfaces displaySubInterfaces(netInf); out.printf(" "); } } static void displaySubInterfaces(NetworkInterface netInterface) throws SocketException { // Build sub networks Enumeration subInterfaces = netInterface.getSubInterfaces(); // Stating the subnetwork list once more for (NetworkInterface subInterface : Collections.list(subInterfaces)) { out.printf(" Sub Interface Display name: %s ", subInterface.getDisplayName()); out.printf(" Sub Interface Name: %s ", subInterface.getName()); } } }
Output:
Display name: lo Name: lo