Three methods to obtain the computer Nic using Java

Source: Internet
Author: User

Now our project is now added with authentication. by collecting the CPU number, hard disk number, and network card number of the client computer, we will generate a machine code and use this machine code as the identity, verify the validity of the client. Our project is based on the RCP architecture, so we use a plug-in such as SWT extension (developed by Chinese people and a member of blogjava), which is convenient, there is no problem in obtaining the CPU number and hard disk number. Some computers cannot collect the information when obtaining the NIC information. The collection method is as follows:

Int [] Macs = extension. getmacaddress (0 );
If (MACS = NULL | Macs. Length = 0 ){
Macs = extension. getmacaddress (1 );
}
If (MACS = NULL | Macs. Length = 0 ){
Macs = extension. getmacaddress (2 );
}
If (MACS = NULL | Macs. Length = 0 ){
Macs = extension. getmacaddress (3 );
}
Stringbuffer = new stringbuffer ();
For (INT I = 0; I <Macs. length; I ++ ){
Stringbuffer. append (gethexstring (MACS [I], 2)
If (I! = Macs. Length-1)
Stringbuffer. append ("-");
}
System. Out. println (stringbuffer. tostring. touppercase ()););

Note that the NIC address obtained through the extension. getmacaddress () method is a decimal array, which must be converted to hexadecimal format to be consistent with the current information of the operating system.
This collection method is not omnipotent. It is estimated that it has something to do with the operating environment. Our project has around 4000 computers by client. Most of the installed Windows 2000 operating systems and Java runtime environments are JRE 6.0, after analysis, we found that some computers use dual NICs. The most common computer I have ever seen has nine NICs (including virtual and disabled ones ), in this case, SWT extension cannot work normally, and other methods such

String command = "cmd.exe/C ipconfig/all ";
PROCESS p = runtime.getruntime(cmd.exe C (command );
Bufferedreader BR = new bufferedreader (New inputstreamreader (P. getinputstream ()));
String line;
While (line = Br. Readline ())! = NULL ){
System. Out. println ("line:" + line );
If (line. indexof ("physical address")> 0 ){
Int Index = line. indexof (":");
Index + = 2;
Address = line. substring (INDEX );
Break;
}
}
BR. Close ();

Parse information from the returned input stream. This method does not work either !. Here we will focus on the third method: Get it through the method in JDK 6.0:

Private Static string getmac (){
Try {
Enumeration <networkinterface> El = networkinterface. getnetworkinterfaces ();
While (El. hasmoreelements ()){
Byte [] Mac = El. nextelement (). gethardwareaddress ();
If (MAC = NULL)
Continue;
Stringbuilder builder = new stringbuilder ();
For (byte B: Mac ){
Builder. append (hexbyte (B ));
Builder. append ("-");
}
Builder. deletecharat (builder. Length ()-1 );
Return builder. tostring ();
}
}
Catch (exception ){
Exception. printstacktrace ();
}
Return NULL;
}

This is a new feature of JDK 6.0. It supports multi-nic configuration. You only need to judge it.
My approach is to use these three methods at the same time: if the first method cannot be obtained, instead of the second method, and the second method does not work, I will use the third method, if the third method still does not work, we recommend that you reinstall the system: upgrade to the XP system.

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.