English 中文(简体)
单例java方案[复制]
原标题:check for single instance java program [duplicate]
  • 时间:2011-11-22 07:47:28
  •  标签:
  • java
  • swing

我在贾瓦有一个方案(有一个支部),我只想有一个例子。 如果它试图打开另一个方案的例子,我就希望把目前的事例带到地下。

我如何这样做?

提前感谢。

最佳回答

采用 Java Webstart and implementing the SingleInstanceservice/code> of the demo. of the SingleInstanceservice

如果它试图打开另一个方案的例子,我就希望把目前的事例带到地下。

Hook that up in the newActivation(String[] of thecode>SingleInstanceListener。 将通过为新发射提供的任何论点。 现有案例可以决定与新的动力是什么(例如,变更档案,增加新的表格,忽视)。

问题回答

您可以使用ShutDownHook和24个档案,见这一简单的

I think that it is the simplest way ...

Java没有先入手,但你可以在(或/瓦尔/伦)的温室簿上创建假档案。 (And make it File.deleteOnExit ( to Clean it anyway onwal)

To bring the existing window to top, you may notify the program yourself, thru named pipe, unix socket, or java remote method call, etc. A simple & dirty way is to write to a small file, say $TEMP/foobar-app.bring-to-top, and the program should periodically poll this small file, if it comes to exist, bring the window to top and remove this small file.

I guess Javast处理信号,即:lu-HUP PID可能不适用于 Java的申请。 即便是这样,也并非每个顾问都有信号。

我曾用一份Socket和一份服务器名册这样做:


首先,在您开始申请时,请在以下几个港口听取意见:ServerSocket。 trick是为了检查它是否扔下了<条码>。 如果是的话,则有另一项申请,或者另一个申请使用该港口(TCP Socket(保证您的联系到达;UDP确实有t)。

例如:

ServerSocket ss = null;
try {
    ss = new ServerSocket(4004);
} catch (IOException ex0) {
    // Port either occupied by your application or a foreign one
    // -> Connect
    Socket s = null;
    try {
        s = new Socket();
    } catch (Exception ex1) {
        // Something went wrong
    }
    if (s != null) {
        // Send some singnal
    }
}
if (ss == null) {
    // Close or do something else
}

(我从我的记忆中写道,某些事情可能是错误的,或者可以做得更好)。

在C#中,你通常在Applicaiton开始创建Mutex。 如果你不能建立/提出,则另一个申请的例子已经开始。 不幸的是,如果在 Java采取同样的做法,或者确切的辛迪加的话,我就不敢肯定。

希望这一帮助。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签