如何实现单实例Java应用程序? [英] How to implement a single instance Java application?

查看:28
本文介绍了如何实现单实例Java应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时我会看到许多应用程序,例如 msn、windows media player 等,它们都是单实例应用程序(当用户在应用程序运行时执行时,不会创建新的应用程序实例).

在 C# 中,我为此使用了 Mutex 类,但我不知道如何在 Java 中执行此操作.

解决方案

如果我相信 文章,作者:

<块引用>

让第一个实例尝试在本地主机接口上打开一个监听套接字.如果它能够打开套接字,则假定这是要启动的应用程序的第一个实例.如果不是,则假设此应用程序的实例已在运行.新实例必须通知现有实例已尝试启动,然后退出.现有实例在收到通知后接管并向处理该操作的侦听器触发事件​​.

注意:Ahe 在评论中提到使用 InetAddress.getLocalHost() 可以棘手:

<块引用>
  • 它在 DHCP 环境中无法正常工作,因为返回的地址取决于计算机是否可以访问网络.
    解决方案是打开与 InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
    的连接可能与 错误 4435662 相关.

  • 我还发现了 错误 4665037,它报告的结果超出了 getLocalHost:返回机器的IP地址,对比实际结果:返回127.0.0.1.
<块引用>

令人惊讶的是 getLocalHost 在 Linux 上返回 127.0.0.1 而在 Windows 上却没有.

<小时>

或者你可以使用 ManagementFactory 对象.正如所解释的 此处:

<块引用>

getMonitoredVMs(int processPid) 方法接收当前应用程序PID作为参数,并捕获从命令行调用的应用程序名称,例如,应用程序是从c启动的:javaapp est.jar 路径,则值变量为c:\java\app\test.jar".这样,我们将在下面代码的第 17 行捕获应用程序名称.
之后,我们在JVM中搜索另一个同名的进程,如果找到并且应用程序PID不同,则说明是第二个应用程序实例.

JNLP 还提供了一个 SingleInstanceListener

Sometime I see many application such as msn, windows media player etc that are single instance applications (when user executes while application is running a new application instance will not created).

In C#, I use Mutex class for this but I don't know how to do this in Java.

解决方案

If I believe this article, by :

having the first instance attempt to open a listening socket on the localhost interface. If it's able to open the socket, it is assumed that this is the first instance of the application to be launched. If not, the assumption is that an instance of this application is already running. The new instance must notify the existing instance that a launch was attempted, then exit. The existing instance takes over after receiving the notification and fires an event to the listener that handles the action.

Note: Ahe mentions in the comment that using InetAddress.getLocalHost() can be tricky:

  • it does not work as expected in DHCP-environment because address returned depends on whether the computer has network access.
    Solution was to open connection with InetAddress.getByAddress(new byte[] {127, 0, 0, 1});
    Probably related to bug 4435662.

  • I also found bug 4665037 which reports than Expected results of getLocalHost: return IP address of machine, vs. Actual results : return 127.0.0.1.

it is surprising to have getLocalHost return 127.0.0.1 on Linux but not on windows.


Or you may use ManagementFactory object. As explained here:

The getMonitoredVMs(int processPid) method receives as parameter the current application PID, and catch the application name that is called from command line, for example, the application was started from c:javaapp est.jar path, then the value variable is "c:\java\app\test.jar". This way, we will catch just application name on the line 17 of the code below.
After that, we search JVM for another process with the same name, if we found it and the application PID is different, it means that is the second application instance.

JNLP offers also a SingleInstanceListener

这篇关于如何实现单实例Java应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆