简体   繁体   中英

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

如何在Linux,Windows和Mac上使用Java + JNI检索硬盘的唯一ID?

概括起来:您不能仅使用Java来做到这一点

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.

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.

I may be wrong, imho, this canNot be done without using JNI.

Build your app in two parts

  1. Native component that will use either a script/application to query the hardware, and output to a file
  2. Your java app to read from the file and do whatever

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM