4

I know you can do this with Java 6 using java.net.NetworkInterface->getHardwareAddress(). But the environment I am deploying on is restricted to Java 5.

Does anyone know how to do this in Java 5 or earlier? Many thanks.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Dave
  • 267
  • 2
  • 4
  • 12
  • Not an answer (hence commenting), but if there's no built-in way, you could always grab the code from Java 6 and backport it. The source is available. – T.J. Crowder Aug 26 '09 at 09:08
  • 1
    It's a native call in Java 6 so backport is not going to work. Getting Mac address without the context of a connection can be meaningless. In some cases, you could get fake Mac addresses used by virtual devices. Those addresses are not globally unique. Several VPN drivers, AOL client are known to install such devices with fixed Mac on Windows. They rarely popup to the first device but it happened to us before. – ZZ Coder Aug 26 '09 at 10:12
  • Which MAC address do you want when the host has eight of them? – John Saunders Sep 03 '09 at 14:03
  • 1
    Not sure. What one would be best? – Dave Sep 08 '09 at 10:04

4 Answers4

6

The standard way in Java 5 was to start a native process to run ipconfig or ifconfig and parse the OutputStream to get your answer.

For example:

private String getMacAddress() throws IOException {
    String command = “ipconfig /all”;
    Process pid = Runtime.getRuntime().exec(command);
    BufferedReader in = new BufferedReader(new InputStreamReader(pid.getInputStream()));
    Pattern p = Pattern.compile(”.*Physical Address.*: (.*)”);
    while (true) {
        String line = in.readLine();
        if (line == null)
            break;
        Matcher m = p.matcher(line);
        if (m.matches()) {
            return m.group(1);
        }
    }
}
butterchicken
  • 13,583
  • 2
  • 33
  • 43
3

Butterchicken's solution is ok, but will only work on english versions of Windows.

A somewhat better (language independent) solution would be to match the pattern for MAC addresses. Here I also make sure that this address has an associated IP (e.g. to filter out bluetooth devices):

public String obtainMacAddress()
throws Exception {
    Process aProc = Runtime.getRuntime().exec("ipconfig /all");
    InputStream procOut = new DataInputStream(aProc.getInputStream());
    BufferedReader br = new BufferedReader(new InputStreamReader(procOut));

    String aMacAddress = "((\\p{XDigit}\\p{XDigit}-){5}\\p{XDigit}\\p{XDigit})";
    Pattern aPatternMac = Pattern.compile(aMacAddress);
    String aIpAddress = ".*IP.*: (([0-9]*\\.){3}[0-9]).*$";
    Pattern aPatternIp = Pattern.compile(aIpAddress);
    String aNewAdaptor = "[A-Z].*$";
    Pattern aPatternNewAdaptor = Pattern.compile(aNewAdaptor);

    // locate first MAC address that has IP address
    boolean zFoundMac = false;
    boolean zFoundIp = false;
    String foundMac = null;
    String theGoodMac = null;

    String strLine;
    while (((strLine = br.readLine()) != null) && !(zFoundIp && zFoundMac)) {
        Matcher aMatcherNewAdaptor = aPatternNewAdaptor.matcher(strLine);
        if (aMatcherNewAdaptor.matches()) {
            zFoundMac = zFoundIp = false;
        }
        Matcher aMatcherMac = aPatternMac.matcher(strLine);
        if (aMatcherMac.find()) {
            foundMac = aMatcherMac.group(0);
            zFoundMac = true;
        }
        Matcher aMatcherIp = aPatternIp.matcher(strLine);
        if (aMatcherIp.matches()) {
            zFoundIp = true;
            if(zFoundMac && (theGoodMac == null)) theGoodMac = foundMac;
        }
    }

    aProc.destroy();
    aProc.waitFor();

    return theGoodMac;
}
Carles Barrobés
  • 11,608
  • 5
  • 46
  • 60
  • This only works for Windows. Is there a better solution for getting OS independent MAC address? – Buhake Sindi May 14 '12 at 09:49
  • Not in Java5, unless you rewrite this method to work in e.g. Unix and call one or the other based in system property "os.name". In Java6 you can. But the question is about Java5 – Carles Barrobés May 14 '12 at 12:46
  • True, hence I'm iterating. This solution only works for JDK 5 in Windows but not other OS'es. – Buhake Sindi May 14 '12 at 16:03
  • I'll re-iterate then: write an equivalent function that calls "ifconfig -a" and parses the result - you will probably need to make adjustments to make it work on different flavours of Unix/Linux/OSX. Call the Windows version or the *nix version depending on os.name. – Carles Barrobés May 14 '12 at 21:04
  • In case there are several adapters: Should method obtainMacAddress() get a parameter for an IP address of a dedicated adapter? – Sam Ginrich Mar 03 '22 at 10:47
  • aIpAddress will take only one digit of last octet. – Sam Ginrich Mar 04 '22 at 11:48
2

As far as I know there is no pure pre Java 6 solution. UUID solves this but first determine OS to find out if it should run ifconfig or ipconfig.

Jonas Elfström
  • 30,834
  • 6
  • 70
  • 106
1

On Linux and Mac OS X Machine , you might have to use ifconfig -a.
ipconfig is as windows command.

Florent
  • 12,310
  • 10
  • 49
  • 58
gmsk19
  • 49
  • 1
  • 6