Java Get MAC Address

Source: Internet
Author: User
Tags date1

The following is the source code:

package test; importjava.io.bufferedreader;importjava.io.file;importjava.io.ioexception;  importjava.io.inputstream; importjava.io.inputstreamreader; importjava.io.linenumberreader;  Importjava.util.date; publicclass test {    public static string  getmacaddresswithcmd ()  {    String mac = null;     try {        date date1 = new date ();         process pro = runtime.getruntime (). EXEC (" Cmd.exe /c ipconfig/all ");         inputstream is =  pro.getinputstream ();        bufferedreader br =  New bufferedreader (New inputstreamreader (is,              "UTF-8"); &nBsp;       string message = br.readline ();         int index = -1;         while  (message != null)  {        if  (index  = message.indexof ("physical address"))  > 0)  {             mac = message.substring (index + 36). Trim ();             break;         }        message = br.readline ();     }        br.close ();         pro.destroy ();        date date2 =  New date ();   &Nbsp;     system.out.println ("Getmacaddresswithcmd:"  +  (Date2.gettime ()- Date1.gettime ())  +  "MS");    } catch  (ioexception e)  {         system.out.println ("Can ' t get mac address!");         return null;    }         return mac;    }    public  Static string getmacaddresswithip (STRING&NBSP;IP)  {         String str =  "";         string macaddress  =  "";        try {             date date1 = new date ();             string scancmd =  "c:\\windows\\system32\\nbtstat.exe -a ";// 32-bit system             File file = new  File ("C:\\windows\\syswow64");            //  64-bit Systems             if  (file.exists ())  {             scancmd =  "c:\\windows\\ sysnative\\nbtstat.exe -a  ";        }         process p = runtime.getruntime (). exec (SCANCMD&NBSP;+&NBSP;IP);         InputStreamReader ir = new  InputStreamReader (P.getinputstream ());        linenumberreader  Input = new linenumberreader (IR);        for  (int i = 1; i < 100 ;  i++)  {            str =  Input.readline ();            if  (str !=  null)  {                 if  (Str.indexof ("mac address")  > 1)  {                     macAddress =  Str.substring (                     str.indexof ("Mac address")  + 14, str.length ());                     break;                 }             }    }    date date2 =  new date ();     system.out.println ("Getmacaddresswithip:"  +  ( Date2.gettime ()-date1.gettime ())  +  "MS");    } catch  (IOException  e)  {        e.printstacktrace (System.out);     }        return macaddress;    }     public static void main (String[] args)  {         system.out.println ("Mac address:"  + getmacaddresswithip (" 192.168.0.124 "));         system.out.println (" Mac address: " +  getmacaddresswithcmd ());     }} 

Getmacaddresswithcmd executes faster and getmacaddresswithip much slower.

The role of Sysnative

In fact, the%windir%\sysnative folder does not exist, it is only a 64-bit Windows system provides a redirection mechanism. We already know that 64-bit Windows uses the System32 and SysWoW64 two-piece folders to differentiate between 64-bit and 32-bit system files, and when 32-bit applications try to access the System32 folder, the system automatically transfers it to the SysWoW64 folder. This allows 32-bit applications to run on both 32-bit systems and 64-bit systems (without the need to change the System32 to SysWoW64 for a 64-bit system). So there is a problem, how do 32-bit applications access the real System32 folder, which is the folder where the 64-bit system files are stored? The answer is through the Sysnative folder. This folder does not exist, which is not found in explorer, but when a 32-bit application tries to access the folder, 64-bit Windows redirects it to the true System32 folder, providing a way for 32-bit applications to access the 64-bit system files. Please refer to MSDN for specific details.

Java Get MAC Address

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.