P
Phil Hanna
Operating system specific, of course, but this works for Windows NT:U¿ytkownik Shripathi Kamath napisa³:
Oh... Sorry about that
This "not necessarily" seemed to be referring to "Sounds like you need some
JNI and platform specific code".
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.regex.*;
public class GetMac
{
public static void main(String[] args)
throws IOException
{
String address = new GetMac().getMacAddress();
System.out.println(address);
}
public String getMacAddress() throws IOException
{
String macAddress = null;
String command = "ipconfig /all";
Process pid = Runtime.getRuntime().exec(command);
BufferedReader in =
new BufferedReader(
new InputStreamReader(pid.getInputStream()));
while (true) {
String line = in.readLine();
if (line == null)
break;
Pattern p = Pattern.compile(".*Physical Address.*: (.*)");
Matcher m = p.matcher(line);
if (m.matches()) {
macAddress = m.group(1);
break;
}
}
in.close();
return macAddress;
}
}