Java Program to Get System MAC Address of Windows machine
A Media Access Control (MAC) address is a unique identifier given to a Network Interface Controller (NIC) for communication purposes within a network segment. It is represented in a hexadecimal format and serves as a network address in various IEEE 802 networking technologies like Ethernet, Wi-Fi, and Bluetooth. Each NIC is assigned a distinct MAC address, allowing devices to identify and communicate with each other on the same network. The MAC address plays a crucial role in ensuring the proper transmission and reception of data packets within a network. The above java codes are used to find out the Mac address from the windows pc.
Example 1
In the above code, we used the InetAddress and NetworkInterface classes from the java.net package to retrieve the MAC address of the Windows machine. It starts by getting the local host's InetAddress using InetAddress.getLocalHost().
FileName: SystemMacAddress.java
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; public class SystemMacAddress { public static void main(String[] args) { try { // Get the local host's InetAddress InetAddress localhost = InetAddress.getLocalHost(); // Retrieve the NetworkInterface associated with the local host NetworkInterface networkInterface = NetworkInterface.getByInetAddress(localhost); // Get the MAC address as an array of bytes byte[] macBytes = networkInterface.getHardwareAddress(); // Build the MAC address string StringBuilder macAddress = new StringBuilder(); // Iterate over each byte of the MAC address for (int i = 0; i < macBytes.length; i++) { // Convert the byte to a hexadecimal string and append it to the StringBuilder macAddress.append(String.format("%02X", macBytes[i])); // Add a colon (":") between each pair of hexadecimal digits, except for the last pair if (i < macBytes.length - 1) { macAddress.append(":"); } } // Print the MAC address System.out.println("MAC Address: " + macAddress.toString()); } catch (UnknownHostException | SocketException e) { e.printStackTrace(); } } }
Output
MAC Address: 18:47:3C:9B:51:60
Example 2 (For Multiple Mac Addresses)
In the above code, we use NetworkInterface.getNetworkInterfaces() to retrieve all the network interfaces associated with the system. We iterate through each network interface and obtain its MAC address if it is not null. The program then constructs and prints the MAC address string for each valid network interface found. This way, you can retrieve multiple MAC addresses if the system has more than one network interface.
FileName: SystemMacAddress1.java
import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Enumeration; public class SystemMacAddress1 { public static void main(String[] args) { try { // Retrieve all network interfaces associated with the system Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); // Iterate over each network interface while (networkInterfaces.hasMoreElements()) { NetworkInterface networkInterface = networkInterfaces.nextElement(); // Get the MAC address of the current network interface byte[] macBytes = networkInterface.getHardwareAddress(); // Check if MAC address exists for the current network interface if (macBytes != null) { // Build the MAC address string StringBuilder macAddress = new StringBuilder(); // Iterate over each byte of the MAC address for (int i = 0; i < macBytes.length; i++) { // Convert the byte to a hexadecimal string and append it to the StringBuilder macAddress.append(String.format("%02X", macBytes[i])); // Add a colon (":") between each pair of hexadecimal digits, except for the last pair if (i < macBytes.length - 1) { macAddress.append(":"); } } // Print the MAC address System.out.println("MAC Address: " + macAddress.toString()); } } } catch (SocketException e) { e.printStackTrace(); } } }
Output
MAC Address: 18:47:3C:9B:51:60 MAC Address: 1A:47:3C:9B:51:92 MAC Address: 14:CB:19:07:91:63 MAC Address: 18:47:3C:9B:51:92
Example 3
The above code executes the ipconfig /all command using the Runtime.getRuntime().exec() method to obtain the network configuration information of the Windows machine. It then reads the output of the command and searches for the line that contains the MAC address.
FileName: SystemMacAddress3.java
import java.io.BufferedReader; import java.io.InputStreamReader; public class SystemMacAddress3 { public static void main(String[] args) { try { // Execute the ipconfig /all command Process process = Runtime.getRuntime().exec("ipconfig /all"); // Create a BufferedReader to read the command output BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line; String macAddress = null; // Search for the MAC address in the ipconfig output while ((line = reader.readLine()) != null) { if (line.trim().startsWith("Physical Address")) { // Extract the MAC address substring macAddress = line.substring(line.indexOf(":") + 2); break; } } reader.close(); // Print the MAC address if found, or display a message if not found if (macAddress != null) { System.out.println("MAC Address: " + macAddress); } else { System.out.println("MAC Address not found."); } } catch (Exception e) { e.printStackTrace(); } } }
Output
MAC Address: 18:47:3C:9B:51:60