2

我正在编写一个 Eclipse 插件并且项目环境配置正确(我相信)。

我能够在没有任何编译时错误的情况下编译解决方案。以下是在调试解决方案时抛出异常的代码片段。我确信所有必需的插件和外部 jar 都已正确配置。

我已经尝试了用户在互联网上建议的几乎所有方法,但没有任何效果。(我什至也根据他们的建议更改了代码)。

因此,请您让我知道我低于错误的原因并建议替代方法。

public static IProject getCurrentSelectedProject() {
        try {
            IWorkspace workspace = ResourcesPlugin.getWorkspace();
            IProject[] projects = workspace.getRoot().getProjects();
            for (int i = 0; i < projects.length; i++) {
                IProject project = projects[i];
                if ((project.isOpen())) {
                    return project;
                }
            }
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
        return null;

    }

以下是我收到的错误

java.lang.NoClassDefFoundError: org/eclipse/core/resources/ResourcesPlugin
    at com.prithvi.editor.actions.UploadAction.getCurrentSelectedProject(UploadAction.java:81)
    at com.prithvi.editor.actions.UploadAction.run(UploadAction.java:39)
    at org.eclipse.ui.internal.PluginAction.runWithEvent(PluginAction.java:251)
    at org.eclipse.ui.internal.WWinPluginAction.runWithEvent(WWinPluginAction.java:229)
    at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:584)
    at org.eclipse.jface.action.ActionContributionItem.access$2(ActionContributionItem.java:501)
    at org.eclipse.jface.action.ActionContributionItem$5.handleEvent(ActionContributionItem.java:411)
    at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
    at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
    at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4169)
    at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3758)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1029)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:923)
    at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
    at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
    at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
    at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
    at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
    at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
    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:353)
    at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
    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:629)
    at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
    at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
    at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
Caused by: java.lang.ClassNotFoundException: org.eclipse.core.resources.ResourcesPlugin
    at org.eclipse.osgi.internal.loader.BundleLoader.findClassInternal(BundleLoader.java:501)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:421)
    at org.eclipse.osgi.internal.loader.BundleLoader.findClass(BundleLoader.java:412)
    at org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader.loadClass(DefaultClassLoader.java:107)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 33 more
4

2 回答 2

7

-> 打开 Plugin.xml -> 依赖项选项卡 -> 点击添加 ->org.eclipse.core.resources

于 2014-08-15T13:28:52.380 回答
4

当您启动应用程序时,您必须在启动配置中指定应加载哪些插件(连同您自己的插件),或者如果您有产品配置,则仅启动该产品配置中包含的插件和功能。

通常这工作得很好,因为 Eclipse 可以识别不同插件之间的依赖关系,因此会自动添加所有必要的插件。但是, org.eclipse.core.resources 是少数例外之一,您必须在这 3 个位置之一手动添加它:

  • 启动配置
  • 或产品配置
  • 或在您的插件的依赖项中(这是首选)
于 2012-11-23T05:51:17.097 回答