tags:

views:

957

answers:

6

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

+1  A: 

Do you mean a memory address, the IP address(es) or the MAC address(es)?

Steve Moyer
A: 
try {
    InetAddress addr = InetAddress.getLocalHost();

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

    // Get hostname
    String hostname = addr.getHostName();
} catch (UnknownHostException e) {
}
Joe Dean
This is the IP address, not the physical (MAC) address.
Matthew Flaschen
+3  A: 

You need Java 6

check out http://www.kodejava.org/examples/250.html

l_39217_l
Just what I was looking for
A: 

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
+2  A: 

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
A: 

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.

bmatthews68