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,475
  • 28
  • 40
  • 37
chepseskaf
  • 654
  • 2
  • 12
  • 39
  • How is your code? Do you convert byte to integer to display the mac as String? – cheffe Sep 26 '13 at 06:45
  • Possible duplicate http://stackoverflow.com/questions/6164167/get-mac-address-on-local-machine-with-java – Abhijith Nagarajan Sep 26 '13 at 08:19
  • @AbhijithNagarajan, I know how to retrieve network information. My code is working on Windows XP or Debian. The Subject is this java API does not provides correct information on Windows 7. – chepseskaf Sep 26 '13 at 09:12
  • How do you determine the interface to check for the mac address? When I enumerate all the interfaces I get the correct addresses as reported by `ipconfig /all`. – Petesh Sep 26 '13 at 11:28
  • @Petesh I do not filter on any interface, I'm displaying all information about all interfaces. Do you have any virtual interfaces too ? – chepseskaf Sep 27 '13 at 04:42
  • I have plenty of virtual interfaces - The only difference is that the order is not the same as the command line. – Petesh Sep 27 '13 at 07:16
  • @Petesh Wich java version are you using ? – chepseskaf Sep 27 '13 at 08:11
  • I have tried java 1.6 u31 (x86, 64bits), 1.7 u21, u40 (x86, 64bits), and I have the same issue. – chepseskaf Sep 30 '13 at 05:12

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