0

I'm displaying network information using a very simple code that uses Java API: NetworkInterface#getHardwareAddress() .

The code is working on Windows XP, XP 64, Debian.

I find two different behaviors on Win 7: computer of my company vs mine. Information displayed are not the same as ipconfig /all, I get only the physical address of the last virtual network card.

I reproduce the issue using java 1.6 u32, 1.7 u21 and 1.7 u40 (both versions x86/64): looking at the output, eth3 and eth4 return the wrong mac address.

I think the code is correct: this is the same as suggested on Stack Overflow and the result is correct on my personal computer.

  • Does anyone know what parameters might influence the result?
  • What settings should I check on Windows to determine differences between different machines?
  • Any suggestions ?

ToDo

I will try to disable virtual interfaces then relaunch the tool. (Needs the IT intervention...).

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
chepseskaf
  • 664
  • 2
  • 12
  • 41

1 Answers1

0

I have the same problem. Here is the code which works on my Windows 7 machine with VMVare virtual cards:

private String getMacJava5() throws Exception {
    String mac = "";

    InetAddress ip = InetAddress.getLocalHost();

    String[] command = {"ipconfig", "/all"};
    Pattern physAddr = Pattern.compile("\\s*Physi.*: (.*)");
    Pattern ipAddr = Pattern.compile("\\s*IPv.*: ([^(]*).*");

    Process p = Runtime.getRuntime().exec(command);
    BufferedReader inn = new BufferedReader(new InputStreamReader(p.getInputStream()));
    while (true) {
        String line = inn.readLine();
        if (line == null) {
            break;
        }

        Matcher mm = physAddr.matcher(line);
        if (mm.matches()) {
            mac = mm.group(1);
        }
        mm = ipAddr.matcher(line);
        if (mm.matches()) {
            if (mm.group(1).equals(ip.getHostAddress())) {
                break;
            }
            mac = "";
        }
    }
    return mac + " IP: " + ip.getHostAddress();
}
Alexei
  • 1
  • I have tried this solution, but it is not cross platform. What about linux or OSX ? Is `ifconfig` authorized for non sudoers ? – chepseskaf Oct 17 '13 at 07:14