Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there!

When I launch a process (let´s call it "OnlyYou.jar") I need to kill all the other running instances of it, no matter who launched them - myself or any other user.
(The reason: this process needs to be monitored by another app and the monitor doesn´t work well with multiple instances.)

But it seems that Java doesn´t allow that a user be able to see processes running under other accounts. Right ?

I did an experience getting the PID of the foreign instance running the "tasklist" command and passing it to the app, but (as expected) this hasn´t any effect.
This PID keeps not being recognized under my user.

Any suggestions ? Or is this question impossible to be solved in Java ?

Thanks !
Posted
Comments
Jochen Arndt 18-Jan-16 9:56am    
If there is a way to do this, your process must run as root/administrator or be elevated because only those accounts can kill processes started by other users.

A common solution to such problems is doing the opposite:
Deny starting the application if another instance is already running.

To detect this some kind of mutex can be created. If another instance tries to create the same mutex, that will fail.

1 solution

Killing a process started by another user requires executing the kill command as root/administrator which is usually not an option.

But you may implement some kind of IPC (Inter Process Communication) in your application that reacts on a specific command to terminate the process. An example would be using a TCP or UDP server listening on a localhost port.

Another common solution is doing the opposite:
Deny starting the application if another instance is already running.
This can be detected by using some kind of mutex or a lock file.

A lock file can be also used with the IPC method to check if another process needs to be terminated.

Pseudo code:
if lock file exists
    send terminate command via socket
    wait until lock file has been deleted
create lock file
start server to listen for termination command

server:
if termination command received
    cleanup (stop server)
    delete lock file
    terminate
 
Share this answer
 
Comments
emersony 19-Jan-16 11:34am    
Jochen,

Good, you gave me some nice ideas.
Using some kind of global object can really be a solution.

Many thanks !

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900