Eclipse RCP:如何防止多个RCP实例启动? [英] Eclipse RCP: How to prevent multiple instances of RCP from launching?

查看:171
本文介绍了Eclipse RCP:如何防止多个RCP实例启动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 http://eclipse.geekyramblings.net/上阅读2010/12/14 / preventing-multiple-instances / 关于如何防止启动RCP的多个实例。但是,当我制作我的RCP(这是一个基于 Hello RCP模板的简单RCP)可执行文件并尝试启动它时,会引发此错误

I read on http://eclipse.geekyramblings.net/2010/12/14/preventing-multiple-instances/ on how to prevent multiple instances of your RCP from being launched. But when I made my RCP(It's a simple RCP based on the "Hello RCP" Template) executable and tried launching it, it throws me this error

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。

I am using Eclipse Java EE IDE for Web Developers.

版本:Helios Service Release 1
构建ID:20100917-0705

Version: Helios Service Release 1 Build id: 20100917-0705

这是我的应用程序中的代码。 Java文件

Here is the code which is in my Application.java file

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();
            }
        });
    }
}

解决此问题的任何帮助

谢谢,

阿巴斯

推荐答案

启动应用程序时,您也可以打开服务器套接字。如果打开另一个实例,请与服务器通信。如果可以进行通信,则可以打开该正在运行的实例,而只剩下一个应用程序实例。

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.

更多详细信息此处

这篇关于Eclipse RCP:如何防止多个RCP实例启动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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