Java get MAC address of Computer

    /**
     * This method get the MAC address of a computer
     *
     * @return string of MAC address of computer
     */
    public static String getMacAddres() {
        String macAddress = "";
        try {
            for (Enumeration<NetworkInterface> enm = NetworkInterface.getNetworkInterfaces(); enm.hasMoreElements();) {
                NetworkInterface network1 = (NetworkInterface) enm.nextElement();
                if (null != network1.getHardwareAddress()) {
                    byte[] mac = new byte[50];

                    mac = network1.getHardwareAddress();

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

                    macAddress = sb.toString();
                }
            }
        } catch (SocketException e) {
            // e.printStackTrace();
        }

        return macAddress;
    }

1 comment:

  1. Hello There

    Allow me to show my gratitude bloggers. You guys are like unicorns. Never seen but always spreading magic. Your content is yummy. So satisfied.

    After successful login of application using angular cli routing "/src/" is adding to the link how to remove this.
    Actual need: https://localhost/angularproject/home/------

    But link coming as : https://localhost/src/angularproject/home/------

    I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.


    Grazie,
    Minny

    ReplyDelete