Java for OS X: Reading a Mac's (unique) Serialnumber

Each Mac has a unique Serialnumber, that you typically only use to check if you are still under warranty - but you could also use it to identify users (analytics) or to make time limited app trials more effective (as Adobe does it).

The Java code is pretty straightforward, basically it runs 'ioreg' and does some string ops:

BufferedReader in;
try {
 String[] command = { "/bin/sh", "-c",
   "ioreg -l | awk '/IOPlatformSerialNumber/ { print $4; }'" };
 Process p = Runtime.getRuntime().exec(command);

 in = new BufferedReader(new InputStreamReader(p.getInputStream()));
 String sn = in.readLine();
 in.close();
 p.destroyForcibly();
 sn = sn.substring(1, sn.length() - 1);// cut off quote. marks

 System.out.println(sn);
} catch (IOException e) {
 e.printStackTrace();
}

Kommentare