3

I have a JavaFX application that should check if it has already been launched when you launch it (need only single instance running at the time). The problem is how to define it!

I have tried packing my JavaFX application into .exe file (or make an .exe launcher) that will be shown as the "MyProg.exe" process in the Windows task manager, not the "javaw.exe". But it is not the solution (I've tried netbeans' tools) because it adds jre to my application thus enlarging it from 1 mb. to 130+ mb. (can you help me with it?)

I've tried launch4j, but both launcher and .exe package starts javaw.exe to run my app. And when I check process manager and see 2 javaw.exe I don't know, is it 2 instances of my app or just another app running?

I hope I described it clearand will be very thankful if somebody can help me with it!

Community
  • 1
  • 1
Chechulin
  • 2,426
  • 7
  • 28
  • 35

1 Answers1

2

Judging by your approach it seems that you are already found a way to get a list of Windows' processes from Java. In this case you can use one of the next solutions:

  1. Take other process info into account. E.g. command line or window title. Windows command line can be retrieved using wmic.exe PROCESS command and for Unix-bases systems by ps -Af.
  2. When your program starts store it's process id in the registry or file in homedir. When another instance starts it should check that value and if process with that id alive then just exit.
Sergey Grinev
  • 34,078
  • 10
  • 128
  • 141
  • 2: what if life of my program will end tragically (taskkill, OS restart) and I won't be able to remove registry entry on exit? Can another javaw.exe get the same PID (it is still possible!) as I have stored in registry, ending up in disability to launch my app? – Chechulin Dec 18 '12 at 09:18
  • @Chechulin: yep, that can be pretty tragic coincidence. But you can't be sure in process id/name/path approach. Someone can use javaw to run jar with exact same name as yours, etc. The almost perfect way would be to introduce a handshake protocol using Sockets or similar interprocess communication options -- thus starting app can validate status of already run one. – Sergey Grinev Dec 18 '12 at 12:00