简体   繁体   中英

How can I get the MAC address of my computer?

I want to get my MAC address. I used following code to do that. But I get output as FF . It is not my mac address. xx:xx:xx:xx:xx:ff is my mac address.

public String getMacAddress() {
    Enumeration<NetworkInterface> networks = null;
    String macaddress=null;
    try {
        networks = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    NetworkInterface inter;
    while (networks.hasMoreElements()) {
        inter = networks.nextElement();
        byte[] mac = null;
        try {
            mac = inter.getHardwareAddress();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        if (mac != null) {
            for (int i = 0; i < mac.length; i++) {
                macaddress=String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
            }
            System.out.println("");
        }
    }
    return macaddress;
}

How can I get my MAC address?

I found this:

package com.mkyong;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;

public class App{

public static void main(String[] args){

InetAddress ip;
try {

    ip = InetAddress.getLocalHost();
    System.out.println("Current IP address : " + ip.getHostAddress());

    NetworkInterface network = NetworkInterface.getByInetAddress(ip);

    byte[] mac = network.getHardwareAddress();

    System.out.print("Current 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 (UnknownHostException e) {

    e.printStackTrace();

   }catch (SocketException e){

    e.printStackTrace();

  }

 }
}

Output:

Current IP address : 192.168.1.22 Current MAC address : 00-26-B9-9B-61-BF

There are 2 problems that I see.

Firstly you are reassigning the value of macaddress . Meaning there will always be just the last byte from mac . The second problem is that you are iterating over all the interfaces your machine has, so even if you changed

macaddress = String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
macaddress += String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");

to append, rather than assign, you would get a string with all the MAC addresses your machine has. Also you would have to initialize macaddress to an empty string (since you cannot append to null ).

Since you are iterating over all interfaces, how about returning a Collection of rather than a single String ? Also using StringBuilder may be more appropriate.

public List<String> getMacAddress() { // Change return type from String to List<String>
    Enumeration<NetworkInterface> networks = null;
    try {
        networks = NetworkInterface.getNetworkInterfaces();
    } catch (SocketException e) {
        e.printStackTrace();
    }
    NetworkInterface inter;
    List<String> addresses = new ArrayList<>(); // Create list to store all MAC addresses
    while (networks.hasMoreElements()) {
        inter = networks.nextElement();
        byte[] mac = null;
        try {
            mac = inter.getHardwareAddress();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        if (mac != null) {
            StringBuilder sb = new StringBuilder(); // Rather than appending with += use a String builder
            for (int i = 0; i < mac.length; i++) {
                sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "")); // Use append on the string builder
            }
            addresses.add(sb.toString()); // Add MAC address to the Collection
        }
    }
    return addresses; // Return collection rather than one String
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM