In the process of writing a program, we sometimes need to know some computer hardware information. For example, when we write a program that needs to be registered, we need to obtain a specific computer information, in general, the physical address of the network card will not be repeated. We can use it as a sign for us to identify a computer. how can we get the physical address of the Nic? We can use the processbuilder class, which is newly added by jdk1.5. We can also use runtime in the past. exce class. here we will demonstrate how to obtain the physical address of the NIC in Windows and Linux environments.
The Code is as follows:
/*
* Test. Java
*
* Created on, 9:11:15
*
* To change this template, choose tools | templates
* And open the template in the editor.
*/
Package Test2;
Import java. Io. bufferedreader;
Import java. Io. ioexception;
Import java. Io. inputstreamreader;
Import java. util. properties;
Import java. util. Logging. level;
Import java. util. Logging. Logger;
/**
*
* @ Author hadeslee
*/
Public class test {
Public static string getmacaddress (){
String address = "";
String OS = system. getproperty ("OS. Name ");
System. Out. println (OS );
If (OS! = NULL ){
If (OS. startswith ("Windows ")){
Try {
Processbuilder Pb = new processbuilder ("ipconfig", "/all ");
PROCESS p = Pb. Start ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (P. getinputstream ()));
String line;
While (line = Br. Readline ())! = NULL ){
If (line. indexof ("physical address ")! =-1 ){
Int Index = line. indexof (":");
Address = line. substring (index + 1 );
Break;
}
}
BR. Close ();
Return address. Trim ();
} Catch (ioexception e ){
}
} Else if (OS. startswith ("Linux ")){
Try {
Processbuilder Pb = new processbuilder ("ifconfig ");
PROCESS p = Pb. Start ();
Bufferedreader BR = new bufferedreader (New inputstreamreader (P. getinputstream ()));
String line;
While (line = Br. Readline ())! = NULL ){
Int Index = line. indexof ("hardware address ");
If (index! =-1 ){
Address = line. substring (index + 4 );
Break;
}
}
BR. Close ();
Return address. Trim ();
} Catch (ioexception ex ){
Logger. getlogger (test. Class. getname (). Log (level. Severe, null, ex );
}
}
}
Return address;
}
Public static void main (string [] ARGs ){
System. Out. println ("" + test. getmacaddress ());
}
}
We can refer to the newly added processbuilderclass. This class is more powerful than runtime.exe C. It can specify an environment variable, specify the directory space when the program is running, and obtain the environment variable when the program is running. because the ipconfig command is already in system32, we do not need to set any other environment variables or specify the runtime directory space of the program. we can use it directly, and then get the output stream of the process to analyze what we need. is it quite simple :)
This program can get the NIC address in Windows and Linux, but if Linux is a Chinese version, the English version is the same, just replace the word. In this way, our program has two platforms.
Original article: http://www.blogjava.net/hadeslee/archive/2007/09/27/148504.html