简体   繁体   English

Linux(Ubuntu)中的Java系统托盘应用程序从服务(守护程序)启动

[英]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. 嗨,我正在为linux(ubuntu)编写一个Java应用程序,它具有托盘应用程序和守护程序应用程序服务。

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) 我能够使用yajsw作为守护程序执行主应用程序,在用户登录到系统后,我希望通知已完成的服务(我已编写脚本并将其放置在/etc/profile.d/文件夹中,脚本正在获取该脚本从那里执行我正在使用TCP套接字连接连接到主应用程序,并传递登录的用户名)

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. 现在,从主应用程序中,我要在当前登录的用户桌面中启动“托盘应用程序”,该应用程序将显示托盘图标( java.awt SystemTray )和弹出菜单。 I have separate jar file created for tray application. 我为托盘应用程序创建了单独的jar文件。

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. Gnome,KDE和Unity均支持在登录时自动启动应用程序。 For example, on Gnome, scripts in ~/.config/autostart/ is automatically started when logging in. 例如,在Gnome上, ~/.config/autostart/脚本在登录时自动启动。

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. 在/etc/X11/Xsession.d/98script下添加了脚本文件,在创建会话后将在用户登录时调用该脚本文件,以便您可以获取$ DISPLAY值,该值对于Java托盘应用程序在会话中启动很重要。 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 从正在监听tcp端口的名为守护程序Java应用程序的脚本中,从守护程序Java应用程序中执行startapp脚本,将已登录的用户名及其DISPLAY传递到startapp脚本中,导出DISPLAY并导出USERNAME并执行将在用户中启动的任务栏应用程序会话和任务栏显示在同一显示区域

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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