使用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

  通過最近幾周的經歷發現,網上的東西都是僅供參考,因為環境的不同、版本的不同會造成很大的差異,而且很多人都只是轉載,從未驗證,告誡自己無論任何東西,一定要切身驗證,

  也要養成寫博客的習慣,把自己的過程記錄記載下來,供別人和自己以后參考。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM