In this tutorial, we will learn how to find mac address using Java.
Introduction :
A MAC (Media Access Control) address is a unique identifier assigned to a network interface controller (N I C) for use as a network address in communications within a network. It is typically represented as a 12-digit hexadecimal number, usually separated by colons or hyphens. The MAC address is used by the network layer to identify and address devices on a network.
Finding the mac address using Java :
In Java, you can retrieve the MAC address of a network interface using the ‘NetworkInterface'
class from the java.net
package. This feature is useful in applications that need to identify network hardware or manage network configurations.
Detailed steps to find the mac address in Java :
- Import Necessary Classes:
java.net.NetworkInterface
: Provides methods for network interface management.java.net.SocketException
: Handles socket-related errors.java.util.Enumeration
: Facilitates enumeration of network interfaces.
- Get Network Interfaces:
- Use
NetworkInterface.getNetworkInterfaces()
to obtain an enumeration of all network interfaces available on the machine.
- Use
- Iterate Through Network Interfaces:
- Loop through the enumeration to process each
NetworkInterface
object.
- Loop through the enumeration to process each
- Get the MAC Address:
- Use the
getHardwareAddress()
method of theNetworkInterface
class to retrieve the MAC address as a byte array.
- Use the
- Format the MAC Address:
- Convert the byte array into a human-readable format, typically as a string of hexadecimal pairs separated by hyphens.
Example code :
import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; public class MacAddressFinder { public static void main(String[] args) { try { // Get an enumeration of all network interfaces Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); while (networkInterfaces.hasMoreElements()) { // Get each network interface NetworkInterface networkInterface = networkInterfaces.nextElement(); // Get the hardware (MAC) address byte[] macAddress = networkInterface.getHardwareAddress(); if (macAddress != null) { // Convert the MAC address byte array to a human-readable string StringBuilder macAddressStr = new StringBuilder(); for (int i = 0; i < macAddress.length; i++) { macAddressStr.append(String.format("%02X", macAddress[i])); if (i < macAddress.length - 1) { macAddressStr.append("-"); } } // Print the network interface name and MAC address System.out.println("Interface: " + networkInterface.getDisplayName() + ", MAC Address: " + macAddressStr.toString()); } } } catch (SocketException e) { // Handle potential SocketException e.printStackTrace(); } } }
Explanation :
- Code Overview : The code is a Java program that finds and prints the MAC addresses of all network interfaces on the machine.
- Main Method : The
main
method is the entry point of the program. It iterates over all network interfaces on the machine usingNetworkInterface.getNetworkInterfaces()
. - Loop through Network Interfaces : For each network interface, it gets the MAC address using
getHardwareAddress()
and checks if it’s not null. - Convert MAC Address to String : If the MAC address is not null, it converts it to a human-readable string format by iterating over each byte and appending it to a
StringBuilder
object with a hyphen (-
) separator. - Print Network Interface Information : Finally, it prints the network interface name and its MAC address using
System.out.println()
. - Error Handling : If any
SocketException
occurs during execution, it prints an error message usinge.printStackTrace()
.
Running the code :
1.Compile the Program:
javac MacAddressFinder.java
2.Run the Program:
java MacAddressFinder
Output :
The output will display the names and MAC addresses of all network interfaces found on the machine. For example:

Related links:
how to find mac address using java
How to convert stack to an array in Java