简体   繁体   中英

Java System Tray Application in Linux (Ubuntu) launch from service (daemon)

Hi I am writing a java application for linux(ubuntu) which has tray application and daemon application service.

I am able to execute the main application as daemon using yajsw, On User Login to the System I want the service to be notified which is have achieved (I have written script and placed in /etc/profile.d/ folder the script is getting executed from there I am connecting to the main application using TCP socket connection and pass the logged in user name)

Now from the main application I want to launch the Tray Application in the current logged in user desktop which will show the tray Icon ( SystemTray of java.awt ) and popup menu. I have separate jar file created for tray application.

One way to go is to launch the tray application in another process

Process p = Runtime.
               getRuntime().
               exec(new String[]{"bash","-c","java -jar /path/to/yourjar"});

Alternatively, you could just use the OS. Gnome, KDE and Unity all supports automatically starting applications at login. For example, on Gnome, scripts in ~/.config/autostart/ is automatically started when logging in.

Personally, I think the best way to go would be

  1. Use the OS to automatically start the system-tray application on login
  2. During initialization, check if the daemon is running from the system-tray application
  3. Display your updates from the daemon, or show a warning if the deamon is not available

Added the script file under /etc/X11/Xsession.d/98script which will be called on user login after session is created so that you can get the $DISPLAY value which is important for the java tray application to start in the session. From the script called the daemon java application which is listening on the tcp port From the daemon java application execute the startapp script pass the logged in username and his DISPLAY in startapp script export DISPLAY and export USERNAME and execute the tray application which will launch in user session and tray is displayed in the same display area

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM