在自己插件的 earlyStartup() 中 Workbench.close() 期间 IDEWorkbenchAdvisor 中的 NullpointerException

NullpointerException in IDEWorkbenchAdvisor during Workbench.close() in earlyStartup() of own plugin

我目前正在编写一个自己的 Eclipse 插件,它应该 运行 在后台运行并在系统中出现文件时自行关闭。

我使用了 org.eclipse.ui.startup 扩展,我正在等待 workbench.isStarting() 为假。但是当我调用 workbench.close() 时,我得到一个 NullPointerException

当前代码:

public class Startup1 implements IStartup {

    @Override
    public void earlyStartup() {


        File file = new File("c:\closeworkbench");

        while(!file.exists()) {
            try {
                Thread.sleep(2000);

            } catch (InterruptedException e) {

            }
        }


        IWorkbench workbench = PlatformUI.getWorkbench();
        while(workbench.isStarting())
        {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }



        }
        workbench.close();

    }

}

这里是 NullpointerException。

!SESSION 2019-05-09 10:13:29.634 -----------------------------------------------
eclipse.buildId=4.11.0.I20190307-0500
java.version=1.8.0_181
java.vendor=Oracle Corporation
BootLoader constants: OS=win32, ARCH=x86_64, WS=win32, NL=de_DE
Framework arguments:  -product org.eclipse.platform.ide
Command-line arguments:  -product org.eclipse.platform.ide -data C:\plugindevworkspace/../runtime-EclipseApplication -dev file:C:/plugindevworkspace/.metadata/.plugins/org.eclipse.pde.core/Eclipse Application/dev.properties -os win32 -ws win32 -arch x86_64 -consoleLog

!ENTRY org.eclipse.egit.core 1 0 2019-05-09 10:13:33.541
!MESSAGE Using Apache MINA sshd as ssh client.
log4j:WARN No appenders could be found for logger (org.eclipse.buildship.core.internal.util.gradle.PublishedGradleVersions).
log4j:WARN Please initialize the log4j system properly.

!ENTRY org.eclipse.egit.ui 2 0 2019-05-09 10:13:38.118
!MESSAGE Warning: The environment variable HOME is not set. The following directory will be used to store the Git
user global configuration and to define the default location to store repositories: 'Y:\'. If this is
not correct please set the HOME environment variable and restart Eclipse. Otherwise Git for Windows and
EGit might behave differently since they see different configuration options.
This warning can be switched off on the Team > Git > Confirmations and Warnings preference page.
It works
Closing workbench

!ENTRY org.eclipse.ui.workbench 4 2 2019-05-09 10:13:39.238
!MESSAGE Problems occurred when invoking code from plug-in: "org.eclipse.ui.workbench".
!STACK 0
java.lang.NullPointerException
    at org.eclipse.ui.internal.ide.application.IDEWorkbenchAdvisor.preShutdown(IDEWorkbenchAdvisor.java:369)
    at org.eclipse.ui.internal.Workbench.busyClose(Workbench.java:1078)
    at org.eclipse.ui.internal.Workbench.lambda(Workbench.java:1413)
    at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:55)
    at org.eclipse.ui.internal.Workbench.close(Workbench.java:1413)
    at org.eclipse.ui.internal.Workbench.close(Workbench.java:1386)
    at de.pds.autoshutdown.Startup1.earlyStartup(Startup1.java:50)
    at org.eclipse.ui.internal.EarlyStartupRunnable.runEarlyStartup(EarlyStartupRunnable.java:80)
    at org.eclipse.ui.internal.EarlyStartupRunnable.run(EarlyStartupRunnable.java:56)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
    at org.eclipse.ui.internal.Workbench.run(Workbench.java:2707)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

!ENTRY org.eclipse.ui 4 0 2019-05-09 10:13:39.240
!MESSAGE Unable to execute early startup code for the org.eclipse.ui.IStartup extension contributed by the 'de.pds.autoshutdown' plug-in.
!STACK 0
java.lang.NullPointerException
    at org.eclipse.ui.internal.ide.application.IDEWorkbenchAdvisor.preShutdown(IDEWorkbenchAdvisor.java:369)
    at org.eclipse.ui.internal.Workbench.busyClose(Workbench.java:1078)
    at org.eclipse.ui.internal.Workbench.lambda(Workbench.java:1413)
    at org.eclipse.swt.custom.BusyIndicator.showWhile(BusyIndicator.java:55)
    at org.eclipse.ui.internal.Workbench.close(Workbench.java:1413)
    at org.eclipse.ui.internal.Workbench.close(Workbench.java:1386)
    at de.pds.autoshutdown.Startup1.earlyStartup(Startup1.java:50)
    at org.eclipse.ui.internal.EarlyStartupRunnable.runEarlyStartup(EarlyStartupRunnable.java:80)
    at org.eclipse.ui.internal.EarlyStartupRunnable.run(EarlyStartupRunnable.java:56)
    at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
    at org.eclipse.ui.internal.Workbench.run(Workbench.java:2707)
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:63)

你有什么想法吗?

earlyStartup 方法未 运行 在 UI 线程中。但是 IWorkbench.close 调用的 IDEWorkbenchAdvisor.preShutdown 方法期望 运行 在 UI 线程中,因为它调用 Display.getCurrent 只在 UI 线程中工作。

您可以尽快尝试使用 Display.asyncExec 来 运行 关闭 UI 线程:

Display.getDefault().asyncExec(() -> workbench.close());

但是我不确定 IWorkbench.isStarted 之类的东西是否是线程安全的,因此您尝试做的事情可能不可靠。