Launch GUI programs from background service in Linux

daemonguijavalinux

I have written a java program to launch various programs in linux with an attached API. I want this java program to run in the background as my user and start automatically on system boot. I have tried running the jar file from a systemd service but when trying to launch GUI applications it fails.

I understand that in order to run a GUI application two environment variables needs to be set:
DISPLAY and XAUTHORITY

I've tried setting them in the systemd service file but the java process still fails to launch any GUI program. There is no problem launching GUI applications when running the jar file from a terminal.

Can this problem be solved with systemd or any other background daemon?

This is what my service file looks like:

[Unit]
Description=Application API
After=network.target

[Service]
User=me
Environment=DISPLAY=:0
Environment=XAUTHORITY=/home/me/.Xauthority
ExecStart=/usr/bin/java -jar /usr/local/bin/windows-application-launcher.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target

Update:
It only needs to run when I'm logged in to my desktop environment.

Best Answer

Start your process as part of the login flow.

  • To add it for all users: add a .desktop file for it in /etc/xdg/autostart (full spec here).
  • To add it just for yourself, add the .desktop in ~/.config/autostart/. Your desktop manager likely has some settings dialog to do this for you -in KDE5 (System Settings): Settings>Workspace>Startup and shutdown>Autostart
Related Question