Last active
          April 28, 2018 03:29 
        
      - 
      
- 
        Save zubayerahamed/0726b713463a194db8ea14f6459c8c0c to your computer and use it in GitHub Desktop. 
    Find PC Mac Address and IP address using java
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 | SocketException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment