How does java get hdd serial number



How to get the serial number of the hard disk in java? This involves cross-platform issues. Different operating systems have different viewing commands, so how to adapt in java? Oshi is used here to obtain.


The full name is nativeoperating system and hardwareinformation. Local calls are made using jna instead of jni.

  • jni
    JNI(Java Native Interface) is a method provided by Java language itself to call local compiled function libraries. It has cross-platform nature and can call different local libraries on different machines. Using JNI to call the. dll/.so shared library, we need to write another. dll/.so shared library in c language, replace the data structure in c language with the data structure specified by SUN, and call the functions published in the existing dll/so. Then load this adapter dll/so in Java, and write Java native function as the proxy of the function in dll. It takes two tedious steps to call local code in Java.

  • jna
    JNA(Java Native Access) framework is an open source spring mvc, developed by SUN Company and based on the classic JNI. Using JNA, you don’t need to write an adaptive. dll/.so, you just need to write an interface and some code in Java, as a proxy for. dll/.so, you can call dll/so in Java programs.



Acquisition method

         SystemInfo si = new SystemInfo();
        HardwareAbstractionLayer hal = si.getHardware();
        HWDiskStore[] stores = hal.getDiskStores(); -> {
            System.out.println(e.getName() + ":" + e.getSerial());