问题
How can I retrieve a hard disk's unique ID using Java+JNI on Linux, Windows and Mac?
回答1:
You could use Java+JNA (https://github.com/twall/jna/), 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.
回答2:
To sum it up: you can't do this with just Java
回答3:
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.
回答4:
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.
回答5:
I may be wrong, imho, this canNot be done without using JNI.
Build your app in two parts
- Native component that will use either a script/application to query the hardware, and output to a file
- Your java app to read from the file and do whatever
来源:https://stackoverflow.com/questions/1244366/how-can-i-retrieve-a-hard-disks-unique-id-using-javajni-on-linux-windows-and