Java Get computer part information

Source: Internet
Author: User

Get MAC address and CPU serial number

Reference Blog: https://www.jb51.net/article/94793.htm

Another reference address is not recorded.

 Packageutil;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileWriter;ImportJava.io.InputStreamReader;Importjava.net.InetAddress;ImportJava.net.NetworkInterface;Importjava.net.SocketException;Importjava.net.UnknownHostException;ImportJava.text.SimpleDateFormat;Importjava.util.Date;ImportJava.util.Scanner;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;/*** @todo Get Computer Configuration information *@authorZhangyanan * @date August 6, 2018*/ Public classCpuutil {Private Static FinalLogger Logger = Loggerfactory.getlogger (cpuutil.class);/*** @todo get the COMPUTER CPU serial number *@authorZhangyanan * @date August 6, 2018*/     Public Staticstring Getcpuserial () {string OS= System.getproperty ("Os.name"); if(Os.tolowercase (). StartsWith ("Win")) {            returncpuutil.getwindowscpuserial (); } Else {            returncpuutil.getlinuxcpuserial (); }    }    /*** @todo windows Get CPU serial number *@authorZhangyanan * @date August 6, 2018*/     Public Staticstring Getwindowscpuserial () {string serial=NULL; Try{Process Process= Runtime.getruntime (). EXEC (NewString[] {"WMIC", "CPU", "get", "Processorid" });            Process.getoutputstream (). Close (); Scanner SC=NewScanner (Process.getinputstream ());            Sc.next (); Serial=Sc.next ();        Sc.close (); } Catch(Exception e) {logger.error ("Getwindowscpuserial Exception", E); }        returnserial; }    /*** Get CPU serial number @todo Linux *@authorZhangyanan * @date August 6, 2018*/     Public Staticstring Getlinuxcpuserial () {string result= ""; Try{File File= File.createtempfile ("tmp", ". vbs");            File.deleteonexit (); FileWriter FW=Newjava.io.FileWriter (file); String VBS= "Set objWMIService = GetObject (\" winmgmts:\\\\.\\root\\cimv2\ ") \ n" + "Set colitems = Objwmiservice.ex    Ecquery _ \ n "+" (\ "SELECT * from win32_processor\") \ n "+" for each objitem in colitems \ n "+" WScript.Echo Objitem.processorid \ n "+" Exit For ' do the first CPU only! \ n "+" Next \ n "; //+ "Exit for \ r \ n" + "Next";Fw.write (VBS);            Fw.close (); String Path= File.getpath (). Replace ("%20", "" "); Process P= Runtime.getruntime (). EXEC ("cscript//nologo" +path); BufferedReader input=NewBufferedReader (NewInputStreamReader (P.getinputstream ()));            String Line;  while(line = Input.readline ())! =NULL) {result+=Line ;            } input.close ();        File.delete (); } Catch(Exception e) {logger.error ("Getlinuxcpuserial Exception", E); }        returnresult; }    /*** @todo Get MAC address *@authorZhangyanan * @date August 6, 2018*/     Public StaticString getmacaddress () {inetaddress ia; Try{ia=Inetaddress.getlocalhost (); returnGetmacaddress (IA); } Catch(unknownhostexception e) {logger.error ("Getmacaddress () exception", E); return NULL; }    }    /*** @todo Get MAC address *@authorZhangyanan * @date August 6, 2018*/     Public StaticString Getmacaddress (inetaddress ia) {//get the Network interface object (that is, the NIC), and get the MAC address, the MAC address exists in a byte array.         Try {            byte[] Mac =Networkinterface.getbyinetaddress (IA). Gethardwareaddress (); //The following code is to assemble the MAC address into a stringStringBuffer SB =NewStringBuffer ();  for(inti = 0; i < mac.length; i++) {                if(I! = 0) {sb.append ("-"); }                //Mac[i] & 0xFF is to convert byte to a positive integerString s = integer.tohexstring (Mac[i] & 0xFF); Sb.append (S.length ()= = 1? 0 +s:s); }            //change all lowercase letters of the string to uppercase to become a regular MAC address and return            returnsb.tostring (). toUpperCase (); } Catch(SocketException e) {logger.error ("Getmacaddress Abnormal!" ", E); return NULL; }    }}

Java Get computer part information

Related Article

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.