如何访问已经运行的java应用程序的引用? [英] How to access the reference of already running java application ?

查看:78
本文介绍了如何访问已经运行的java应用程序的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

We want to design a application , let X.jar
If we first time run it than the new window should open after
that if we run it than no new instance should be create , application must communicate 
with already running instance .





我尝试了什么:



我们要设计一个应用程序,让X.jar

如果我们第一次运行它如果我们运行它而不是应该创建新实例,那么应该打开新窗口,应用程序必须与已经运行的实例进行通信



What I have tried:

We want to design a application , let X.jar
If we first time run it than the new window should open after
that if we run it than no new instance should be create , application must communicate
with already running instance .

推荐答案

这是拥有应用程序的单个实例的经典案例。这可以通过使用 mutex 来实现。



Main中有一个互斥锁类。当应用程序启动时,检查此互斥锁是否可用,如果是,则获取它并启动应用程序。如果没有,只需退出main方法。



现在关闭应用程序时,释放互斥锁。



所以第一个例子:

检查互斥锁。

因为它是免费的,它将获得并启动应用程序。



第二个实例:

检查互斥锁。

它不可用,所以只需退出(可能带有用户消息)。



第一个例子:

关闭时,释放互斥锁,以便下次启动。



这是解决此问题的通用算法。你现在可以找到这个特定的java代码。
This is the classic case of having a single instance of an application. This is achieved by using mutex.

have a mutex in your Main class. when the application starts, check if this mutex is available, if yes then acquire it and start the app. If not, just exit the main method.

Now when you are closing the application, release the mutex.

So the first instance:
check the mutex.
since it is free, it will acquire and start the app.

Second instance:
check the mutex.
its not available, so just exit(possibly with user message).

First instance:
when closing, release the mutex so that next time it could start.

This is the general algorithm to solve this problem. You can find java specific code for this now.


这篇关于如何访问已经运行的java应用程序的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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