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

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

问题描述

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

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).

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

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

推荐答案

如果我相信这个文章,由:


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

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.

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

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



  • 它在DHCP环境中无法正常工作,因为返回的地址取决于计算机是否具有网络访问权限。

    解决方案是用 打开连接InetAddress.getByAddress(new byte [] {127,0,0,1}} ;

    可能与错误4435662 有关。

  • 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.




  • 我还发现错误4665037 报告的结果比的预期结果getLocalHost :返回机器的IP地址,与实际结果相对:返回 127.0.0.1

    • 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.

    • 令人惊讶的是 getLocalHost 返回Linux上 127.0.0.1 但不在Windows上。

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






      或者您可以使用 ManagementFactory 对象。正如所解释的那样这里


      getMonitoredVMs(int processPid)方法接收当前应用程序PID作为参数,并捕获从命令行调用的应用程序名称,例如,应用程序是从 c:\ java /\\\\ test开始的。 jar path,然后值变量为 c:\\\\\\\\\\\\\\\\\\\\\\\\这样,我们将在下面代码的第17行捕获应用程序名称。

      之后,我们在JVM中搜索具有相同名称的另一个进程,如果我们找到它并且应用程序PID不同,这意味着它是第二个应用程序实例。

      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:\java\app\test.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还提供了 SingleInstanceListener

      JNLP offers also a SingleInstanceListener

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

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