将 Java Applet 限制为只有一个实例

发布于 2024-09-10 14:25:47 字数 551 浏览 7 评论 0原文

我已经使用 Stackoverflow 一段时间来寻找我的编程问题的解决方案,但对于当前的问题我还没有找到有用的解决方案。于是我就加入了。

将 Java 小程序限制为仅一个实例的好解决方案是什么?我正在使用 php 向最终用户提供小程序,因此这将是一个选项。

但如果我可以通过 Applet 本身限制 Java Applet 的执行,我会更感兴趣。我对 Java 还很陌生,所以我真的不知道从哪里开始寻找这样的构造。

任何建议将不胜感激。

作为旁注,“ 仅启动 JavaScript具有给定 URL 的 Java 小程序的一个窗口”将是一种解决方案。尽管我将小程序嵌入到我正在构建的应用程序中,但我并不真正热衷于为小程序提供服务的弹出窗口。

真诚地, AltWouss

编辑:
澄清实例限制。我希望每台机器只加载一个小程序。

I have been using Stackoverflow for a while to find solutions to my programming questions, but for the current question I haven't found a useful solution. So I joined up.

What would be a good solution to limit a Java applet to only one instance. I'm using php to serve the applet to the end user, so that would be an option.

But I'm much more interested if I can limit the execution of an Java Applet through the Applet itself. I'm pretty new to Java so I don't really know where to start looking for a construction like that.

Any suggestions would be appreciated.

As a side note, "
javascript to launch only ONE window for a Java applet with a given URL" would be a solution. Although I'm embedding the applet into the application I'm building and I'm not really keen on a popup serving the applet.

Sincerely,
AltWouss

Edit:
To clarify the instance limit. I would like to have only one applet loaded per machine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心凉 2024-09-17 14:25:47

为什么不在您的小程序的众所周知的端口上启动 SocketServer 呢?

如果打开该 SocketServer,可能是因为另一个 SocketServer 已经在运行。然后,您可以显示一条消息来说明这一点。

Why don't you start a SocketServer on a well known port for your applet ?

If opening that SocketServer, it may be due to the fact that another one is already running. Then, you can display a message telling so.

很快妥协 2024-09-17 14:25:47

如果您使用 Java WebStart,那么就有 JNLP SingleInstanceService

If you use Java WebStart, then there is the JNLP SingleInstanceService.

卸妝后依然美 2024-09-17 14:25:47

不幸的是,Java 不支持跨进程锁定的命名互斥锁。但是,您可以通过在小程序启动时在已知位置创建一个具有已知名称的文件来实现原始锁定。如果无法创建该文件,那么您就知道该文件已经存在,因为该应用程序之前已经启动过一次。要确保应用程序关闭时删除文件,只需调用 File.deleteOnExit() 方法即可。

类似于:

if(file.createNewFile()) {
    file.deleteOnExit();
} else {
    throw new Exception("Instance already running!");
}

不是一个完美的解决方案,并且肯定存在一些问题......但这可能足以满足您想要做的事情。也可以看看 FileLock 类可以提供更强大和更合适的解决方案。

Unfortunately, Java does not support named mutexs for locking across processes. However, you could implement a primitive lock by simply creating a File at a know location with a known name when your applet starts. If the file cannot be created, then you know that one already exists because the application has already started once before. To ensure the file is deleted when the application closes, simple call File.deleteOnExit() method.

Something like:

if(file.createNewFile()) {
    file.deleteOnExit();
} else {
    throw new Exception("Instance already running!");
}

Not a perfect solution and certainly has some issues... But it's probably enough for what you are trying to do. Also could look at that FileLock class which could offer a more robust and appropriate solution.

海的爱人是光 2024-09-17 14:25:47

我不太确定这是否有帮助..但我首先想到的是..是否有可能使小程序成为单例?
抱歉,这更像是一个后续问题,而不是一个答案......

i am not really sure of this will help or not.. but the first thing that comes my mind is.. Is it possible to make the applet a singleton?
sorry this is more as a follow up question then an answer...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文