5

I am working in a desktop application created in java. we are working for windows version (for running on windows O.S.) of the application. When we run our application, it creates a tray icon on desktop by using systemtray as

SystemTray tray = SystemTray.getSystemTray();

I am creating single instance of the system tray, which I am creating in the main class of the application.

I am creating exe for my application. My problem is when i am running the exe file of my application, it creates tray icon every time.

I want in my application only one tray icon present on desktop and running as a service. this icon should only get removed, when application is uninstalled. It should run as service in the background.

I am not getting any way which will help me to run my application as a service and there should be only one instance running in the background.

I want to run my application by creating a tray icon as a service and should have single instance present. Please guide me in this issue.

Thanks for your all valuable suggestions in advance.

mort
  • 12,988
  • 14
  • 52
  • 97
Toman
  • 1,156
  • 1
  • 12
  • 29

3 Answers3

2

As Mudassir said, you should add a check when you start your program that tries to find another running instance of your app. This could be done by creating a specific file upon startup, and deleting it on close. If the file is already present when you start your app, it means that another instance is running. You could also use inter-process communications, but it would be more difficult than a simple file.

For the "service" part, you could hide the main window on startup, so your app will run as if it was a windows service.

Bastien Jansen
  • 8,756
  • 2
  • 35
  • 53
  • Thanks for answering i hope this will work for me. Please elaborate some more to service part. – Toman Nov 08 '10 at 14:29
  • I don't think it's possible to create a Windows service in Java, meaning a service which can automatically be run at startup, and which could be managed like other standard Windows services. But you could do something similar by adding an entry in the registry (google something like "windows automatic startup"). When your program is launched, it will check if another instance is running. If not, it will create a system tray icon like you want, and hide its windows (if it's actually using some kind of GUI). – Bastien Jansen Nov 08 '10 at 15:04
1

Make your application Single Instance. And please tell how you are making an .EXE file in Java?

Mudassir
  • 13,031
  • 8
  • 59
  • 87
  • Thanks for answering Mudassir. I am creating exe using advanced installer. Advanced installer uses jar files and creates exe from the jar. – Toman Nov 08 '10 at 13:02
1

For a tray icon to appear, your app must be running. If your tray icon appears to be there twice (or more), than your application runs multiple times. You have to add a check for this yourself.

You don't want to run your app as a windows service, as normally these have no access to the Windows Desktop.

ZeissS
  • 11,867
  • 4
  • 35
  • 50
  • You don't want to run your app as a windows service, as normally these have no access to the Windows Desktop. May be Toman is trying to make some anti virus type program which runs in background and shows their presence via the System Tray icon. – Mudassir Nov 08 '10 at 12:06
  • @Mudassir In this case he should make two apps out of it. One for the windows background service and the second one which connects over Network or sthg else. – ZeissS Nov 08 '10 at 12:10
  • Thanks for your comments. My application is for taking online backup. It takes backup of your selected files and updates automatically on server, hence it is require it should run as a service in the background. I am creating single instance for System tray in the main class of the application but my application is unable to identify whether any instance is already present or not. – Toman Nov 08 '10 at 13:09