java - 使用 dbus 调用 java 方法

标签 java dbus

如何使用 dbus 导出 java 方法或对象?

我写这篇文章是因为官方文档非常糟糕,我花了几个小时才弄清楚如何做到这一点。

理想情况下,DBus 接口(interface)应该放在 java 包中

最佳答案

DBus.java

import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.DBusInterfaceName;    

@DBusInterfaceName("org.printer")
public interface DBus extends DBusInterface {
    //Methods to export
    public void Print(String message);
}

Main.java

import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;

public class Main {    
    public static void main(String[] args) {
        Printer p = new Printer();

        try {
            DBusConnection conn = DBusConnection.getConnection(DBusConnection.SESSION);
            //Creates a bus name, it must contain some dots.
            conn.requestBusName("org.printer");
            //Exports the printer object
            conn.exportObject("/org/printer/MessagePrinter", p);
       } catch (DBusException DBe) {
           DBe.printStackTrace();
           conn.disconnect();
           return;
       }
    }
}

//Printer object, implements the dbus interface and gets
//called when the methods are invoked.
class Printer implements DBus {
    public boolean isRemote() {
        return false;
    }

    public void Print(String message) {
        System.out.println(message);
    }
}

您可以在 shell 中使用 qdbus 来尝试此操作,运行:

qdbus org.printer /org/printer/MessagePrinter org.printer.Print test

关于java - 使用 dbus 调用 java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14791000/

相关文章:

python - 通过 dbus 传递大型数据结构

python - 我如何使用 python 通过 D-Bus 从 Skype 接收事件?

python - 如何监控USB设备插入?

dbus - D总线: Export object without ObjectManager

java - 使用 PlayFramework 发送邮件

java - 在 Eclipse 中加载工作区时出错

java - 用Groovy写一个Maven插件和Java相比有什么优势?

java - 让 FileReader 使用循环读取每四行

Java分段上传到s3

linux - 我如何在 DBus 中显示特定对象路径内的接口(interface)