MAC Address In Java Example

Chapter: Networking Last Updated: 22-09-2018 08:33:53 UTC

Program:

            /* ............... START ............... */
                
import java.net.*;

public class JavaMacAddress {
	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) ? "-" : ""));
			}
			System.out.println(sb.toString());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

                /* ............... END ............... */
        

Output

IP address : 192.168.1.118
MAC address : 80-86-F2-02-A0-2G

Notes:

  • A media access control address (MAC address) is a unique identifier assigned to network interfaces for communications on the physical network segment.
  • Java InetAddress class represents an IP address. The java.net.InetAddress class provides methods to get the IP of any host name.
  • InetAddress.getLocalHost() returns the instance of InetAdddress containing local host name and address.
  • In the above program ip.getHostAddress() returns the IP address machine in string format.
  • NetworkInterface class represents a Network Interface made up of a name, and a list of IP addresses assigned to this interface. It is used to identify the local interface on which a multicast group is joined.
  • network.getHardwareAddress() gives the hardware address (usually MAC) of the interface.

Tags

MAC Address, Java, get all mac addresses,how to get mac address of client machine in java, java get ip address from mac address

Similar Programs Chapter Last Updated
Java URL Parts Example Networking 28-10-2016
Java Program To Create Socket At Specific Port Networking 28-10-2016
Java Find Proxy Settings Networking 10-09-2016
Java Download Webpage Example Networking 09-09-2016
Java URL Connection Date Networking 09-09-2016
Java Socket To Single Client Networking 09-09-2016
Java Server File Size Example Networking 09-09-2016
Java Multithreaded Server Example Networking 09-09-2016
Java Server File Modification Time Networking 09-09-2016
Java Connect To Server Example Networking 09-09-2016
Http Response Header In Java Example Networking 21-06-2016
Server Socket In Java Example Networking 30-04-2016
Socket Programming In Java Networking 30-04-2016
IP Address And HostName Of Local Computer In Java Networking 30-04-2016
HostName And IP Address In Java Networking 30-04-2016
Client Socket In Java Example Networking 30-04-2016
Read And Download Webpage In Java Example Networking 29-04-2016
Date Of URL Connection In Java Example Networking 29-04-2016
Who Is Lookup In Java Example Networking 21-07-2016
System Proxy In Java Example Networking 21-09-2018
Port Scanner In Java Example Networking 21-07-2016
Ping IP Address In Java Example Networking 21-09-2018
Get IP Address In Java Example Networking 07-04-2016
URL Properties In Java Example Networking 09-09-2016
Default URL Object In Java Example Networking 18-07-2016

1