Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check for single instance java program [duplicate]

Tags:

java

swing

I have a program in Java (with a swing gui), and I would like only 1 instance ever to exist. If it attempted to open another instance of the program I would like the current instance to be brought to the foreground.

How do I do this?

Thanks in advance.

like image 961
rustybeanstalk Avatar asked Nov 22 '11 07:11

rustybeanstalk


1 Answers

Launch the application using Java Web Start and implement the SingleInstanceService of the JNLP API. Here is a demo. of the SingleInstanceService.

If it attempted to open another instance of the program I would like the current instance to be brought to the foreground.

Hook that up in the newActivation(String[]) method of the SingleInstanceListener. It will be passed any arguments that were provided for the new launch. The existing instance gets to decide what to do with the new args (e.g. change file, add new tab, ignore..)

like image 189
Andrew Thompson Avatar answered Nov 15 '22 07:11

Andrew Thompson