从 Linux 提取硬盘信息的 Java 代码
我最近的任务是开发一个程序来监控来自多个硬盘的大量硬盘信息(序列号、制造商、类型等)。在我最初的研究中,我发现使用一些 C# 代码可以很容易地做到这一点,并以这种方式创建了一个测试程序。不幸的是,我需要使用 Linux 操作系统,这意味着 c# 是不可能的。
我很好奇是否有人知道,或者可以为我指明用 Java/C/C++ 开发应用程序的正确方向,以达到相同的效果。我确实知道,如果您使用 Java,则必须使用 JNI,并且无论如何您此时都在使用 C/C++。
谢谢。
I've recently been tasked with developing a program to monitor a bunch of hard drive information (serial, maker, type, etc) from multiple hard drives. In my initial research I found that this was pretty easy to do using some C# code and created a test program that way. Unfortunately, I'm required to use a linux OS which means c# is off the table.
I was curious if anyone knows, or could point me in the right direction of developing an app in Java/C/C++ that would achieve the same effect. I do know that if you use Java you have to use the JNI and you're using C/C++ at that point regardless.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据您需要的具体信息,可能不需要使用 JNI。
Linux 在 sysfs 和 procfs 虚拟文件系统。例如,您可以在 /sys/block/sda/device/model 中找到第一个磁盘的硬盘型号,您应该能够像任何其他文件一样在 Java 中读取该型号。
此外,您还可以查看 fdisk -l /dev/sda' 和 hdparm -I /dev/sda 的输出,它们都提供了大量信息。您可以使用 Runtime.exec()` 调用它们并解析输出。
Depending on the exact information you need there might be no need to use JNI.
Linux make a lot of information about the hardware available in the sysfs and procfs virtual filesystems. For instance, you can find the harddisk model for the fist disk in /sys/block/sda/device/model, which you should be able to read in Java just like any other file.
Additionally you could look at the output of
fdisk -l /dev/sda' and
hdparm -I /dev/sdawhich both provide quite a bit of information. You can call these using
Runtime.exec()` and parse the output.我不知道有任何 Java 库可以让您的生活更轻松,但如果您想实现支持不同操作系统的独立于操作系统的解决方案,您应该看看 java.lang.Runtime 使您能够执行外部进程。您可以为每个操作系统维护一个映射,定义用于检索所需信息的 (CLI) 命令。它可能比使用 JNI 实现一堆本机调用更容易:
我知道这不是一个完美的解决方案,但您可能想看看它。
I'm not aware of any Java libraries making your life easier, but if you want to implement an OS independent solution supporting different OSes you should have a look at java.lang.Runtime which enables you to execute external processes. You could maintain a map for each OS defining the (CLI) commands for retrieving the required information. It may be easier than implementing a bunch of native calls using JNI:
I know it's not a perfect solution, but you may want to have a look at it.