Article Title: Obtain the NIC physical address (Windows and Linux) in JAVA ). Linux is a technology channel of the IT lab in China. Some basic categories include desktop applications, Linux system management, kernel research, embedded systems, and open source. In the process of program writing, we sometimes need to know some computer hardware information, for example, when we write a program to be registered, we need to obtain the specific information of a computer. Generally, the physical address of the NIC will not be repeated, we can use it 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 the Runtime. exce class in the past. Here we will demonstrate how to obtain the physical address of the NIC in Windows and Linux environments.
/** 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 getMA CAddress () {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.