检查单实例java程序 [英] check for single instance java program
问题描述
我有一个Java程序(带有一个swing gui),我想只有一个实例存在。如果它试图打开另一个程序实例,我希望将当前实例带到前台。
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.
我该怎么做?
提前致谢。
推荐答案
使用Java Web Start 并实现 SingleInstanceService
.oracle.com / javase / 7 / docs / jre / api / javaws / jnlp / index.htmlrel =nofollow noreferrer> JNLP API 。这是一个演示。 SingleInstanceService
。
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.
将其挂钩在 SingleInstanceListener
的newActivation(String [])方法。它将传递为新发布提供的任何参数。现有实例决定如何处理新的args(例如更改文件,添加新标签,忽略..)
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..)
这篇关于检查单实例java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!