Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- public class MacAddress {
- public static void main(String[] args) {
- try {
- InetAddress address = InetAddress.getLocalHost();
- NetworkInterface ni =
- NetworkInterface.getByInetAddress(address);
- if (ni != null) {
- byte[] mac = ni.getHardwareAddress();
- if (mac != null) {
- for (int i = 0; i < mac.length; i++) {
- System.out.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 (UnknownHostException e) {
- e.printStackTrace();
- } catch (SocketException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment