tags:

views:

505

answers:

5

How can I retrieve a hard disk's unique ID using Java+JNI on Linux, Windows and Mac?

+3  A: 

To sum it up: you can't do this with just Java

Tommy
I agree but there must some way to do it ?????
DD
@DD: either you agree, or you understand that there is no way to do that.
Yuval A
Sure you can do it. But then you have to have some scripts giving java it's output. It's not very platform independent.
Tommy
A: 

As already in indicated, you can't within the boundaries of the question. However, you might be able to do it with a combination of java and native code specific for each platform via the JNI layer.

Jim Rush
+1  A: 

I do not think there is a simple, uniform way to do that.

You can however create seperate logic for all cases; on linux you could check /proc (using the java.io package). There are probably similar ways on OS X and Windows, or, if not, you could execute a shell script or batch file on these systems and parse the output.

Or you could use JNI, though that would mean building your module for all environments.

extraneon
A: 
Everyone
A: 

You could use Java+JNA (https://jna.dev.java.net/), but then you'd have to figure out how to gather that information by using native libraries on each of the platforms you'd like to support.

The benefit is that you wouldn't have to compile any C/C++ code for each of the platforms. If you decide to go with that option someone else might be able to tell you how to figure out the harddisk IDs on the different platforms using C/C++ code/libraries.

AFAIK, on Linux you need to read something from /proc or /sys, on Windows I would look through MSDN and see what you could find that is usable in Visual Studio (C++) and for Mac someone else would have to fill in. Solaris/BSD should probably be supported too if you do it right. In fact, for most of the POSIX-compatible OSes out there I think you should be able to do it somewhat uniformly.

Robin Smidsrød