Java read machine address

Dear all, i am planning to use the machine address to be the unique character to determine my licensing program.

I have code this code,

      Enumeration enu = NetworkInterface.getNetworkInterfaces();
                  while (enu.hasMoreElements()){
                        System.out.println(enu.nextElement());
                  }
..
..

**********
name:lo (MS TCP Loopback interface) index: 1 addresses:
/127.0.0.1;

name:eth0 (Realtek RTL8168/8111 PCI-E Gigabit Ethernet NIC - Packet Scheduler Miniport) index: 2 addresses:
/11.11.10.29;

name:eth1 (Bluetooth PAN Network Adapter - Packet Scheduler Miniport) index: 65540 addresses:
/121.145.50.1;

*****************

and it's doesn't seems what i am looking for.
What i am looking is something which is unique and the best in mind is the Network Interface Card number.

Can you help??

penang67Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
cmalakarConnect With a Mentor Commented:
This will work only in jdk 1.6 .. !
InetAddress netaddr = InetAddress.getLocalHost();
NetworkInterface intf = NetworkInterface.getByInetAddress(netaddr);
byte[] b = intf.getHardwareAddress();
 
for(int i = 0; i < b.length; i++)
{
  String temp = Integer.toHexString(b[i] & 0xff);
  temp = temp.length() > 1 ? temp : "0" + temp;
  System.out.print(temp + " "); //Prints the bytes of mac address
}

Open in new window

0
 
cmalakarCommented:
InetAddress netaddr = InetAddress.getLocalHost();
netaddr.getHostName(); // will return the hostname
netaddr.getHostAddress(); // will return the host ip address
0
 
cmalakarCommented:
or else You can use..

getHardwareAddress()  api of NetworkInterface class
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
penang67Author Commented:
the  InetAddress doesn't have the get MAC address method.
0
 
cmalakarCommented:
getHardwareAddress() returns the MAC Address

But it is available from jdk 1.6 version..

If you want to get the MAC Address in < 1.6 version..

Then you have execute os command using

Runtime.exec("oscommand") and grep from the MAC address from the output
0
 
penang67Author Commented:
can you show how to get it from getHardwareAddress() from the inetaddr???
0
 
penang67Author Commented:
cmalakar, thanks for your help. Can you help to explain, how is this statement mean??

 String temp = Integer.toHexString(b[i] & 0xff);

the toHexString(int i) how come become b[i] & 0xff and why '0Xff'??

thanks
0
 
cmalakarCommented:
0
All Courses

From novice to tech pro — start learning today.