Linux Java U盘插入监听:使用JNA库实现实时监控

在Linux系统上使用Java监听插入U盘,可以使用Java的JNA库来实现。以下将详细介绍步骤和代码示例:

1. 安装JNA库

可以使用Maven或Gradle等构建工具来安装JNA库。

Maven:

<dependency>
    <groupId>net.java.dev.jna</groupId>
    <artifactId>jna</artifactId>
    <version>5.2.0</version>
</dependency>

Gradle:

implementation 'net.java.dev.jna:jna:5.2.0'

2. 编写代码

使用JNA库可以调用Linux系统的udev库,监听USB设备的插入和拔出。

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.Structure;
import com.sun.jna.ptr.PointerByReference;

import java.util.Arrays;
import java.util.List;

public class USBListener {

    public interface UdevLibrary extends Library {
        UdevLibrary INSTANCE = Native.load("udev", UdevLibrary.class);

        Pointer udev_new();

        void udev_unref(Pointer udev);

        Pointer udev_monitor_new_from_netlink(Pointer udev, String name);

        void udev_monitor_enable_receiving(Pointer udev_monitor);

        int udev_monitor_get_fd(Pointer udev_monitor);

        Pointer udev_monitor_receive_device(Pointer udev_monitor);

        String udev_device_get_devnode(Pointer udev_device);

        String udev_device_get_property_value(Pointer udev_device, String key);

        String udev_device_get_subsystem(Pointer udev_device);

        String udev_device_get_action(Pointer udev_device);

        void udev_device_unref(Pointer udev_device);
    }

    public static void main(String[] args) {
        Pointer udev = UdevLibrary.INSTANCE.udev_new();
        if (udev == null) {
            System.err.println("Failed to initialize udev");
            System.exit(1);
        }

        Pointer udev_monitor = UdevLibrary.INSTANCE.udev_monitor_new_from_netlink(udev, "udev");
        if (udev_monitor == null) {
            System.err.println("Failed to initialize udev monitor");
            System.exit(1);
        }

        UdevLibrary.INSTANCE.udev_monitor_enable_receiving(udev_monitor);
        int fd = UdevLibrary.INSTANCE.udev_monitor_get_fd(udev_monitor);

        while (true) {
            PointerByReference udev_device_ref = new PointerByReference();
            int result = Native.poll(new NativePollFd[]{new NativePollFd(fd, (short) Native.POLLIN, (short) 0)}, -1);
            if (result > 0) {
                Pointer udev_device = UdevLibrary.INSTANCE.udev_monitor_receive_device(udev_monitor);
                if (udev_device != null) {
                    String subsystem = UdevLibrary.INSTANCE.udev_device_get_subsystem(udev_device);
                    String action = UdevLibrary.INSTANCE.udev_device_get_action(udev_device);
                    if ('block'.equals(subsystem) && 'add'.equals(action)) {
                        String devnode = UdevLibrary.INSTANCE.udev_device_get_devnode(udev_device);
                        String vendorId = UdevLibrary.INSTANCE.udev_device_get_property_value(udev_device, 'ID_VENDOR_ID');
                        String productId = UdevLibrary.INSTANCE.udev_device_get_property_value(udev_device, 'ID_MODEL_ID');
                        System.out.println('USB device inserted: ' + devnode + ' (' + vendorId + ':' + productId + ')');
                    }
                    UdevLibrary.INSTANCE.udev_device_unref(udev_device);
                }
            }
        }
    }

    public static class NativePollFd extends Structure {
        public int fd;
        public short events;
        public short revents;

        public NativePollFd() {
            super();
        }

        public NativePollFd(int fd, short events, short revents) {
            super();
            this.fd = fd;
            this.events = events;
            this.revents = revents;
        }

        @Override
        protected List<String> getFieldOrder() {
            return Arrays.asList('fd', 'events', 'revents');
        }
    }
}

该代码使用了poll系统调用来监听udev监视器的文件描述符。当有USB设备插入时,会从udev监视器中接收到一个udev设备对象,并从中获取设备节点、厂商ID和产品ID等信息。

3. 运行代码

可以使用命令行编译并运行代码:

javac -cp .:jna-5.2.0.jar USBListener.java
java -cp .:jna-5.2.0.jar USBListener

运行后,插入U盘时将会输出类似以下的信息:

USB device inserted: /dev/sdb1 (0951:1625)

**注意:**要运行此代码需要使用root权限或者在udev规则中添加相应的权限。

总结

本教程详细介绍了如何在Linux系统上使用Java的JNA库监听U盘插入事件。通过该方法,可以实时监控U盘的插入和拔出,并获取相关的设备信息。在实际应用中,可以根据需要对代码进行进一步扩展和改进。

Linux Java U盘插入监听:使用JNA库实现实时监控

原文地址: https://www.cveoy.top/t/topic/n2m5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录