Java - Program to get System IP, name and MAC Address of windows system.


R.N. 08 August 2016

In this code snippet I am going to tell how to get System IP Address, System Name and System MAC Address of Windows System.

Java Code Snippet - Get System IP, System Name and System MAC Address in Java

package com.includehelp;

import java.net.InetAddress;
import java.net.NetworkInterface;

/**
 * Program to get System IP, name and MAC Address of  windows system
 */
public class SystemInfo {

    public static void main(String[] args) {
        System.out.println("Host Name : "+getSystemName());
        System.out.println("Host IP   : "+getIPAddress());
        System.out.println("Host Address : "+getMAC());
    }
    
    /**
     * Method for get System Name
     * @return  Host name
     */
    private static String getSystemName(){  
        try{
            InetAddress inetaddress=InetAddress.getLocalHost(); //Get LocalHost refrence
            String name = inetaddress.getHostName();   //Get Host Name
            return name;   //return Host Name
        }
        catch(Exception E){
            E.printStackTrace();  //print Exception StackTrace
            return null;
        }
    }
    
    /**
     * method to get Host IP
     * @return Host IP Address
     */
    private static String getIPAddress(){
         try{
            InetAddress inetaddress=InetAddress.getLocalHost();  //Get LocalHost refrence
            String ip = inetaddress.getHostAddress();  // Get Host IP Address
            return ip;   // return IP Address
        }
        catch(Exception E){
            E.printStackTrace();  //print Exception StackTrace
            return null;
        }
        
    }
    
    /**
     * method to get Host Mac Address
     * @return  Mac Address
     */
    private static String getMAC(){
         try{
            InetAddress inetaddress=InetAddress.getLocalHost(); //Get LocalHost refrence
            
            //get Network interface Refrence by InetAddress Refrence
            NetworkInterface network = NetworkInterface.getByInetAddress(inetaddress); 
            byte[] macArray = network.getHardwareAddress();  //get Harware address Array
            StringBuilder str = new StringBuilder();
            
            // Convert Array to String 
            for (int i = 0; i < macArray.length; i++) {
                    str.append(String.format("%02X%s", macArray[i], (i < macArray.length - 1) ? "-" : ""));
            }
            String macAddress=str.toString();
		
            return macAddress; //return MAc Address
        }
        catch(Exception E){
            E.printStackTrace();  //print Exception StackTrace
            return null;
        } 
    }
    
}
    

    Host Name : DESKTOP-16DO59R
    Host IP   : 192.168.1.79
    Host Address : 48-E2-44-90-57-A5



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.