Java Utililty Methods Mac Address Get

List of utility methods to do Mac Address Get

Description

The list of methods to do Mac Address Get are organized into topic(s).

Method

StringgetMac()
get Mac
StringBuilder sb = new StringBuilder();
try {
    InetAddress ip = InetAddress.getLocalHost();
    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
    byte[] mac = network.getHardwareAddress();
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
} catch (SocketException | UnknownHostException e) {
return sb.toString();
StringgetMacAddr()
Retrive mac address for current machine
String macAddr = "";
String str = "";
try {
    NetworkInterface nic = NetworkInterface.getByName("eth0");
    if (nic != null) {
        byte[] buf = nic.getHardwareAddress();
        if (buf != null) {
            for (int i = 0; i < buf.length; i++) {
...
StringgetMacAddress()
get Mac Address
String result = "";
try {
    for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        byte[] hardwareAddress = ni.getHardwareAddress();
        if (hardwareAddress != null) {
            for (int i = 0; i < hardwareAddress.length; i++)
                result += String.format((i == 0 ? "" : "-") + "%02X", hardwareAddress[i]);
            return result;
...
StringgetMACAddress()
get MAC Address
StringBuilder sb = new StringBuilder();
try {
    NetworkInterface network = NetworkInterface.getByInetAddress(ip);
    byte[] mac = network.getHardwareAddress();
    for (int i = 0; i < mac.length; i++) {
        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
} catch (SocketException e) {
...
StringgetMacAddress()
get Mac Address
String os = System.getProperty("os.name");
try {
    if (os.startsWith("Windows")) {
        return windowsParseMacAddress(windowsRunIpConfigCommand());
    } else if (os.startsWith("Linux")) {
        return linuxParseMacAddress(linuxRunIfConfigCommand());
    } else {
        throw new IOException("unknown operating system: " + os);
...
byte[]getMACAddress()
Returns the MacAddress of the first physical network interface or a default MacAddress if the there are no network interfaces.
if (macAddress == MAC_ADDRESS_DEFAULT) {
    try {
        Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
        byte[] mac = null;
        while (interfaces.hasMoreElements() && (mac == null || mac.length != 6)) {
            NetworkInterface netInterface = interfaces.nextElement();
            if (netInterface.isLoopback() || netInterface.isVirtual()) {
                continue;
...
StringgetMacAddress()
get Mac Address
if (macAddress == null) {
    try {
        fetchMacAddress();
    } catch (Exception e) {
        e.printStackTrace();
    if (macAddress == null) {
        macAddress = "unknown";
...
StringgetMacAddress()
Returns the MAC address of the first network interface that is found.
try {
    String mac = null;
    for (NetworkInterface ni : Collections.list(NetworkInterface.getNetworkInterfaces())) {
        if (ni.getHardwareAddress() != null) {
            byte[] hwAddr = ni.getHardwareAddress();
            if (hwAddr.length > 0) {
                mac = "";
                for (int i = 0; i < hwAddr.length; i++) {
...
byte[]getMacAddress()
Grabs the mac address of computer and makes it 10 times longer
if (cachedMacAddress != null && cachedMacAddress.length >= 10) {
    return cachedMacAddress;
try {
    Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
    while (networkInterfaces.hasMoreElements()) {
        NetworkInterface network = networkInterfaces.nextElement();
        byte[] mac = network.getHardwareAddress();
...
StringgetMacAddress()
get Mac Address
return getMacAddress(InetAddress.getLocalHost());