Java无法使用getHardwareAddress()获取Ubuntu设备的MAC地址

Java无法使用getHardwareAddress()获取Ubuntu设备的MAC地址,java,jakarta-ee,ubuntu,tomcat6,mac-address,Java,Jakarta Ee,Ubuntu,Tomcat6,Mac Address,我有下面的代码来提取我的Ubuntu设备的MAC地址&我将MAC地址放入数据库。这段代码在windows PC上运行良好,而在Ubuntu设备上则不起作用。数据库字段变为空。我的应用程序是一个web应用程序。我把war文件放在我的Ubuntu设备中的tomcat服务器上 public static String getMACAddress() { StringBuffer strMac = new StringBuffer(); try { InetAdd

我有下面的代码来提取我的Ubuntu设备的MAC地址&我将MAC地址放入数据库。这段代码在windows PC上运行良好,而在Ubuntu设备上则不起作用。数据库字段变为空。我的应用程序是一个web应用程序。我把war文件放在我的Ubuntu设备中的tomcat服务器上

 public static String getMACAddress()
  {
    StringBuffer strMac = new StringBuffer();
    try {

        InetAddress address = InetAddress.getLocalHost();
        // InetAddress address = InetAddress.getByName("192.168.46.53");

        /*
         * Get NetworkInterface for the current host and then read the
         * hardware address.
         */
        NetworkInterface ni = NetworkInterface.getByInetAddress(address);
        if (ni != null) {
            byte[] mac = ni.getHardwareAddress();
            if (mac != null) {
                /*
                 * Extract each array of mac address and convert it to hexa
                 * with the following format 08-00-27-DC-4A-9E.
                 */
                for (int i = 0; i < mac.length; i++) {
                    System.out.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : "");
                    strMac.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "" : ""));
                }
            } else {
                System.out.println("Address doesn't exist or is not accessible.");
            }
        } else {
            System.out.println("Network Interface for the specified address is not found.");
        }
    } catch (Exception e) {

    }
    return strMac.toString();
         }
公共静态字符串getMACAddress()
{
StringBuffer strMac=新的StringBuffer();
试一试{
InetAddress=InetAddress.getLocalHost();
//InetAddress=InetAddress.getByName(“192.168.46.53”);
/*
*获取当前主机的NetworkInterface,然后读取
*硬件地址。
*/
NetworkInterface ni=NetworkInterface.getByInetAddress(地址);
如果(ni!=null){
字节[]mac=ni.getHardwareAddress();
如果(mac!=null){
/*
*提取每个mac地址数组并将其转换为hexa
*采用以下格式08-00-27-DC-4A-9E。
*/
for(int i=0;i
我也提到了这个()链接

我的Ubuntu设备使用LAN和Wifi连接

希望我的问题清楚。请帮忙。。。
谢谢

这几乎是Linux/Windows的翻版,但它与Linux/Windows相反,所以我想我会尝试回答这个问题

看起来您在address变量中得到了环回接口,它没有注册为具有硬件地址,因为它与硬件没有关联。请尝试确保获得与网络硬件关联的InetAddress。链接线程有一个遍历列表的示例