2

How do I get the physical addresses of my machine in Java?

Alireza
  • 100,211
  • 27
  • 269
  • 172
user16949
  • 21
  • 1
  • 3
  • Can you be more specific? Which address? The memory address? IPv4 (or IPv6) address? MAC address? – nsayer Sep 18 '08 at 22:11

4 Answers4

2

As of Java 6, java.net.NetworkInterface class now has the method getHardwareAddress()

http://java.sun.com/javase/6/docs/api/java/net/NetworkInterface.html#getHardwareAddress()

If that's too new, there are UUID packages which try various methods per OS to ask for it. Try e.g. http://johannburkard.de/blog/programming/java/MAC-address-lookup-using-Java.html

wnoise
  • 9,764
  • 37
  • 47
0
try {
    InetAddress addr = InetAddress.getLocalHost();

    // Get IP Address
    byte[] ipAddr = addr.getAddress();

    // Get hostname
    String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}
Joe Dean
  • 3,356
  • 5
  • 26
  • 31
0

I think this might be what you're looking for, in the Java API for the InetAddress class: http://java.sun.com/javase/6/docs/api/java/net/InetAddress.html

getLocalHost() 
zxcv
  • 7,391
  • 8
  • 34
  • 30
0

If you need you need the MAC address you are going to require JNI. I use a library called JUG to generate UUIDs based using the real MAC address of the machine. You can consult their source code to see how this is accomplished on Linux, Solaris, Windows and Mac platforms.

Brian Matthews
  • 8,506
  • 7
  • 46
  • 68