Eclipse RCP:如何防止启动多个 RCP 实例?
我读过 http://eclipse.geekyramblings.net/2010/12 /14/preventing-multiple-instances/ 了解如何防止启动 RCP 的多个实例。但是当我制作我的 RCP(这是一个基于“Hello RCP”模板的简单 RCP)可执行文件并尝试启动它时,它向我抛出了
java.io.IOException: The location has not been set.
at org.eclipse.core.runtime.internal.adaptor.BasicLocation.lock(BasicLocation.java:174)
at testrcp.Application.start(Application.java:43)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:619)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:574)
at org.eclipse.equinox.launcher.Main.run(Main.java:1407)
我正在使用的 错误 面向 Web 开发人员的 Eclipse Java EE IDE。
版本:Helios 服务版本 1 构建 ID:20100917-0705
这是我的 Application.java 文件中的代码
package testrcp;
import org.eclipse.core.runtime.Platform;
import org.eclipse.equinox.app.IApplication;
import org.eclipse.equinox.app.IApplicationContext;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
/**
* This class controls all aspects of the application's execution
*/
public class Application implements IApplication {
/* (non-Javadoc)
* @see org.eclipse.equinox.app.IApplication#start(org.eclipse.equinox.app.IApplicationContext)
*/
public Object start(IApplicationContext context) throws Exception {
Display display = PlatformUI.createDisplay();
int exitCode = IApplication.EXIT_OK;
Location instanceLocation = Platform.getInstanceLocation();
if (!instanceLocation.lock()) {
MessageDialog.openError(new Shell(display), "App Title",
"Another instance of the Application is currently running.");
} else {
int returnCode = PlatformUI.createAndRunWorkbench(display,
new ApplicationWorkbenchAdvisor());
switch (returnCode) {
case PlatformUI.RETURN_RESTART :
exitCode = IApplication.EXIT_RESTART;
break;
default :
exitCode = IApplication.EXIT_OK;
}
}
instanceLocation.release();
display.dispose();
return exitCode;
}
/* (non-Javadoc)
* @see org.eclipse.equinox.app.IApplication#stop()
*/
public void stop() {
if (!PlatformUI.isWorkbenchRunning())
return;
final IWorkbench workbench = PlatformUI.getWorkbench();
final Display display = workbench.getDisplay();
display.syncExec(new Runnable() {
public void run() {
if (!display.isDisposed())
workbench.close();
}
});
}
}
任何有关解决此问题的帮助或任何建议,我们将不胜感激。
谢谢,
Abbas
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以通过
在之后
添加此行来使其工作,但是请注意,这不会阻止它从 RCP 可执行文件存在的不同文件夹启动...有什么方法可以对此进行检查吗?
I was able to make it work by adding this line
after
But be aware, this will however not prevent it from being launched from a different folder where the RCP executable exists... Is there any way to keep a check on that ?
您还可以在启动应用程序时打开服务器套接字。如果打开另一个实例,则与服务器通信。如果您可以通信,则可以打开该正在运行的实例,留下应用程序的一个实例。
更多详细信息此处
You can also open a server socket when you launch your application. If you open another instance, communicate with the server. If you can communicate, that running instance can be opened leaving one instance of the application.
More in detail here