So I am trying to have a Java program that starts running when the main method is invoked and stops running when the same main method is invoked (from another process). So the goal is to kill the first process with the second process, and then kill itself.
What I have tried are the following:
taskkill
for Windows andkill
for Unix.This is the simplest way to do achieve the goal. But the process in Windows is always
javaw.exe
and callingtaskkill
would terminate other JVMs.
ServerSocket
orDatagramSocket
, to tell the first process to kill itself.This works as well but I feel like this is just a waste for such a simple job.
File locks
The problem is that the permission of creating a file is not always granted (for whatever reason someone decides to use the program in read-only folders).
I think I might just missed something obvious. Any suggestions would be appreciated.