Eclipse RCP:如何防止RCP的多个实例启动?

时间:2011-08-18 08:14:28

标签: eclipse-rcp eclipse-3.6

我在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 Service Release 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();
            }
        });
    }
}

任何有关解决此问题或任何建议的帮助都将非常感激。

谢谢,

阿巴斯

2 个答案:

答案 0 :(得分:1)

我能够通过添加此行

来使其工作
instanceLocation.getURL();

之后

Location instanceLocation = Platform.getInstanceLocation();

但请注意,这不会阻止它从存在RCP可执行文件的其他文件夹中启动...有没有办法对其进行检查?

答案 1 :(得分:1)

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

更详细here