-
Notifications
You must be signed in to change notification settings - Fork 6
Basic usage
When using this library all communication between your application and the systemd service manager is done via D-Bus. Thus at first a connection via either the system or the user (session) bus is required. Both can be achieved via the 'Systemd' class.
The following code establishes a connection via the system bus and disconnects at the end. An exception of type 'DBusException' is thrown in case the connection can't be established.
import de.thjom.java.systemd.Systemd;
import org.freedesktop.dbus.exceptions.DBusException;
Systemd systemd = new Systemd();
try {
systemd.connect();
}
catch (DBusException e) {
// ...
}
systemd.disconnect();Calling the constructor method with the DBusConnection.SYSTEM argument does the same:
import de.thjom.java.systemd.Systemd;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
Systemd systemd = new Systemd(DBusConnection.SYSTEM);
try {
systemd.connect();
}
catch (DBusException e) {
// ...
}
systemd.disconnect();Likewise it is possible to establish a connection via the user (session) bus:
import de.thjom.java.systemd.Systemd;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
Systemd systemd = new Systemd(DBusConnection.SESSION);
try {
systemd.connect();
}
catch (DBusException e) {
// ...
}
systemd.disconnect();The 'Systemd' class implements the 'AutoClosable' interface hence it allows the try-with-resource pattern like shown below which omits the explicit call to disconnect from the bus.
Connection via system bus:
import de.thjom.java.systemd.Systemd;
import org.freedesktop.dbus.exceptions.DBusException;
try (Systemd systemd = Systemd.createAndConnect()) {
// ...
}
catch (DBusException e) {
// ...
}Connection via user (session) bus:
import de.thjom.java.systemd.Systemd;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
try (Systemd systemd = Systemd.createAndConnect(DBusConnection.SESSION)) {
// ...
}
catch (DBusException e) {
// ...
}Once a connection has been established the particular service manager instance can be loaded by accessing the bus. The following