Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Get MAC address with an applet

843790Jul 24 2010 — edited Oct 29 2010
Hello!

I'm trying to get MAC adress of the NIC through which user is connected to the internet.

Example: I call an URL "www.someurl.com/page.html" and applet on this page should resolve MAC address of my network card.

I'm already able to get MACs of all network interfaces:
public void getMacs() {
        try {
            /*
             * Get NetworkInterface for the current host and then read the
             * hardware address.
             */
            Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces();
 
            if (nis != null) {
                while (nis.hasMoreElements()) {
                    NetworkInterface currentNI = nis.nextElement();
                    Enumeration<InetAddress> addresses = currentNI.getInetAddresses();
 
                    while (addresses.hasMoreElements()) {
                        InetAddress inetAddress = addresses.nextElement();
                        
                        byte[] mac = currentNI.getHardwareAddress();
 
                        if (mac != null) {
                            if (mac.length > 0) {
                                String hostName = inetAddress.getHostName();
                                String hostAddr = inetAddress.getHostAddress();
                                System.out.println("HOSTNAME = " + hostName + " HOSTADDR = " + hostAddr);
 
                                System.out.print(
                                    "Name: " + currentNI.getName() + " | " +
                                    "DisplayName: " + currentNI.getDisplayName() + " MAC: ");
 
                                /*
                                 * 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 < mac.length - 1) ? "-" : "");
}
System.out.println();
}
} 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 (SocketException e) {
e.printStackTrace();
}
}
But how I can find out whether *currentNI* is the network interface I'm looking for?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 26 2010
Added on Jul 24 2010
20 comments
22,296 views