Guest User

Untitled

a guest
Feb 2nd, 2018
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. import java.net.InetAddress;
  2. import java.net.NetworkInterface;
  3. import java.net.SocketException;
  4. import java.net.UnknownHostException;
  5.  
  6. public class MacAddress {
  7. public static void main(String[] args) {
  8. try {
  9. InetAddress address = InetAddress.getLocalHost();
  10.  
  11. NetworkInterface ni =
  12. NetworkInterface.getByInetAddress(address);
  13. if (ni != null) {
  14. byte[] mac = ni.getHardwareAddress();
  15. if (mac != null) {
  16.  
  17. for (int i = 0; i < mac.length; i++) {
  18. System.out.format("%02X%s",
  19. mac[i], (i < mac.length - 1) ? "-" : "");
  20. }
  21. } else {
  22. System.out.println("Address doesn't exist or is not " +
  23. "accessible.");
  24. }
  25. } else {
  26. System.out.println("Network Interface for the specified " +
  27. "address is not found.");
  28. }
  29. } catch (UnknownHostException e) {
  30. e.printStackTrace();
  31. } catch (SocketException e) {
  32. e.printStackTrace();
  33. }
  34.  
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment