How to get MAC address in Java

In this tutorial, we are going to see how to get the MAC address of your computer in Java.

A MAC (Media Access Control) address is a unique identifier assigned to network interfaces for communication on the physical network.

The network.getHardwareAddress() method gives the hardware (usually MAC) address of the network interface.

Java Program to get MAC address:

public class Main {
	public static void main(String[] args) {

		InetAddress ip;
		try {
			ip = InetAddress.getLocalHost();
			System.out.println("IP address : " + ip.getHostAddress());
			NetworkInterface network = NetworkInterface.getByInetAddress(ip);
			byte[] mac = network.getHardwareAddress();
			System.out.print("MAC address : ");

			StringBuilder sb = new StringBuilder();
			for (int i = 0; i < mac.length; i++) {
				sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));

		} catch (Exception e) {


IP address :
MAC address : 0A-E6-28-1F-EE-19
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *