Java Program to Get Physical or MAC Address of Windows and Linux Machine Full Project For Beginners


Welcome folks today in this blog post we will be getting the physical and mac address of windows and linux machine or system in java.All the full source code of the application is shown below.




Get Started




In order to get started you need to create the file and copy paste the following code



// Java program to access the MAC address of the
// localhost machine
import java.util.Enumeration;
public class MACAddress {

    // method to get the MAC Address
    void getMAC(InetAddress addr) throws SocketException
        // create a variable of type NetworkInterface and
        // assign it with the value returned by the
        // getByInetAddress() method
        NetworkInterface iface
            = NetworkInterface.getByInetAddress(addr);

        // create a byte array and store the value returned
        // by the NetworkInterface.getHardwareAddress()
        // method
        byte[] mac = iface.getHardwareAddress();
        // convert the obtained byte array into a printable
        // String
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
                "%02X%s", mac[i],
                (i < mac.length - 1) ? "-" : ""));
        // print the final String containing the MAC Address

    // Driver method
    public static void main(String[] args) throws Exception
        // a variable of type InetAddress to store the
        // address of the local host
        InetAddress addr = InetAddress.getLocalHost();
        // instantiate the MACAddress class
        MACAddress obj = new MACAddress();
        System.out.print("MAC Address of the system : ");
        // call the getMAC() method on the current object
        // passing the localhost address as the parameter

Leave a Reply