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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement



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