0

Right now I am using this code to retrieve the MAC Address of a system.

command = "cmd.exe /c ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

This works fine when the user has defined a 'Path' in the environment variable with the value 'Systemroot/system32' but not without this.
So what I am trying to do is use this

command = "cmd.exe %systemroot%/system32/ipconfig /all";
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

But this is not retrieving the MAC Address at all.

Can anyone please let me know what is wrong here or how to retrieve MAC address, the code for which is compatible in java 5 & above?
Is there any alternate solution?
The getHardwareAddress() doesn't work in java 5. I saw this in many posts, which doesn't suit to my app. My app is compatible to all tomcat and all java versions.
Please help...

I referred to and followed this How do I set environment variables from Java? also but without the environment variable the MAC address doesn't get read from the system. Any other suggestions please?

Community
  • 1
  • 1
Freakyuser
  • 2,774
  • 17
  • 45
  • 72

1 Answers1

0

You can get the value of an environment variable using System.getenv, so you could try something along the lines of

File sysRoot = new File(System.getenv("systemroot"));
File ipconfig = new File(new File(sysRoot, "system32"), "ipconfig");
String[] command = new String[] { ipconfig.getAbsolutePath(), "/all" };
Process p = Runtime.getRuntime().exec(command);
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
// read all output from br, then p.waitFor()
Ian Roberts
  • 120,891
  • 16
  • 170
  • 183