使用java获取本机mac

不开心,辛苦写的第一篇文章不小心删除了还恢复不了

想用java 获得本机地址,搜了下,觉得这个看起来不错,简洁

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();

    }

   }

}

  好多都是这个版本,问题在于,一运行,报错了!!很无语,排查了很久也找不到原因,看getHardwareAddress API也没发现错误,很无奈。

mei@mei-X-Series:~/mei/test$ java Ipconfig 
mei-X-Series/127.0.1.1
Exception in thread "main" java.lang.NullPointerException
    at Ipconfig.getLocalMac(Ipconfig.java:24)
    at Ipconfig.main(Ipconfig.java:19)

  终于在http://rupertanderson.com/blog/soapui-google-analytics-null-pointer-exception-on-ubuntu/ 中找到难友,并获知了原因,that NetworkInterface.getByInetAddress cannot match the Machine Name to any of the Network Interface values.就是不匹配了。 

   超出了自己的能力范围,看来智能用复杂的代码了,以下代码测试通过

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class getmac{
public static String getLinuxMACAddress() {
        String mac = null;
        BufferedReader bufferedReader = null;
        Process process = null;
        try {            
            process = Runtime.getRuntime().exec("ifconfig enp4s0");
            bufferedReader = new BufferedReader(new InputStreamReader(
                    process.getInputStream()));
            String line = null;
            int index = -1;
            while ((line = bufferedReader.readLine()) != null) {
                index = line.toLowerCase().indexOf("硬件地址");
                
                if (index != -1) {
                    
                    mac = line.substring(index + 4).trim();
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            bufferedReader = null;
            process = null;
        }
        return mac;
    }
public static void main(String[] argc) {
    String mac = getLinuxMACAddress();
    System.out.println("本地是Linux系统, MAC地址是:" + mac);

}
}

  运行结果如下:

本地是Linux系统, MAC地址是:54:ab:3a:05:34:e6

  通过最近几周的经历发现,网上的东西都是仅供参考,因为环境的不同、版本的不同会造成很大的差异,而且很多人都只是转载,从未验证,告诫自己无论任何东西,一定要切身验证,

  也要养成写博客的习惯,把自己的过程记录记载下来,供别人和自己以后参考。

posted on 2016-12-19 16:21  一念|  阅读(4330)  评论(0编辑  收藏  举报

导航