简体   繁体   English

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

[英]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). 在Linux上,您可以检查/ proc(使用java.io包)。 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. 在OS X和Windows上可能有类似的方法,或者,如果没有,您可以在这些系统上执行Shell脚本或批处理文件并解析输出。

Or you could use JNI, though that would mean building your module for all environments. 或者,您可以使用JNI,尽管这意味着要为所有环境构建模块。

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. 但是,您可以通过JNI层使用特定于每个平台的Java和本机代码的组合来完成此操作。

I may be wrong, imho, this canNot be done without using JNI. 我可能是错的,恕我直言,如果不使用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 您的Java应用程序可从文件读取并执行任何操作

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. 您可以使用Java + JNA( https://github.com/twall/jna/ ),但是随后您必须弄清楚如何通过使用要支持的每个平台上的本机库来收集信息。

The benefit is that you wouldn't have to compile any C/C++ code for each of the platforms. 好处是您不必为每个平台编译任何C / C ++代码。 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. 如果您决定使用该选项,则其他人可能会告诉您如何使用C / C ++代码/库找出不同平台上的硬盘ID。

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. AFAIK,在Linux上,您需要从/ proc或/ sys中读取某些内容,在Windows上,我将仔细研究MSDN,看看可以在Visual Studio(C ++)中找到可用的内容,对于Mac,则需要其他人来填写。Solaris如果操作正确,可能也应该支持/ BSD。 In fact, for most of the POSIX-compatible OSes out there I think you should be able to do it somewhat uniformly. 实际上,对于大多数与POSIX兼容的操作系统,我认为您应该能够做到一定程度的统一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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