AbstractUIPlugin导致NoClassDefFoundError

时间:2013-12-02 21:50:48

标签: java eclipse eclipse-plugin noclassdeffounderror

我正在编写一个支持JUnit等自动化测试的Eclipse插件。我有一个Launch Configuration Delegate,它启动我的Main类,它将在用户提供的类上运行测试。

当我使用Launch Configuration Delegate启动它时,我希望以编程方式显示我的Eclipse插件中的自定义视图。当我尝试启动它时,我继续为AbstractUIPlugin类获取NoClassDefFoundError,即使我在我的插件依赖项中包含了org.eclipse.ui和org.eclipse.core.runtime。

堆栈跟踪

Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/ui/plugin/AbstractUIPlugin
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at eoo.thefamilycoder.specj.internal.launching.delegate.SpecJMain.main(SpecJMain.java:8)
Caused by: java.lang.ClassNotFoundException: org.eclipse.ui.plugin.AbstractUIPlugin
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 13 more

MANIFEST.MF

Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: SpecJ
Bundle-SymbolicName: eoo.thefamilycoder.specj;singleton:=true
Bundle-Version: 1.0.0.alpha
Bundle-Activator: eoo.thefamilycoder.specj.internal.Activator
Require-Bundle: org.eclipse.ui;bundle-version="3.105.0",
 org.eclipse.core.runtime;bundle-version="3.9.0",
 org.eclipse.jdt.launching;bundle-version="3.7.0",
 org.eclipse.debug.core;bundle-version="3.8.0",
 org.eclipse.jdt.core;bundle-version="3.9.1",
 org.eclipse.debug.ui;bundle-version="3.9.0",
 org.eclipse.jdt.ui;bundle-version="3.9.1",
 org.eclipse.jdt.debug.ui;bundle-version="3.6.200"
Bundle-RequiredExecutionEnvironment: JavaSE-1.7
Bundle-ActivationPolicy: lazy

主要课程

import eoo.thefamilycoder.specj.internal.Activator;

public class SpecJMain {

    public static void main(String[] args) {
        Activator.getDefault().showView();
    }

}

导致错误的原因是什么,我该如何解决?

修改:添加启动代码

启动配置代理

public class SpecJLaunchConfigurationDelegate extends AbstractJavaLaunchConfigurationDelegate {

    private static final int UNITS_OF_WORK = 4;

    @Override
    public void launch(final ILaunchConfiguration configuration, final String mode, final ILaunch launch,
            final IProgressMonitor monitor) throws CoreException {
        final IProgressMonitor pm = monitor != null ? monitor : new NullProgressMonitor();

        pm.beginTask(configuration.getName(), UNITS_OF_WORK);
        if (pm.isCanceled()) return;

        try {
            preLaunchCheck(configuration, mode, pm);
            if (pm.isCanceled()) return;

            final VMRunnerConfiguration runConfig =
                new VMRunnerConfigurationFactory().create(configuration, monitor, this);
            if (pm.isCanceled()) return;

            getVMRunner(configuration, mode).run(runConfig, launch, pm);
            if (pm.isCanceled()) return;
        } finally {
            pm.done();
        }
    }

    @Override
    public String[] getClasspath(ILaunchConfiguration configuration) throws CoreException {
        final String[] initialClasspath = super.getClasspath(configuration);

        final String pluginClasspath = new SpecJClasspathLocator().getClasspath();
        System.out.println(pluginClasspath);
        if (pluginClasspath == null) return initialClasspath;

        final String[] classpath = new String[initialClasspath.length + 1];
        classpath[0] = pluginClasspath;
        System.arraycopy(initialClasspath, 0, classpath, 1, initialClasspath.length);

        return classpath;
    }

}

VMRunnerConfigurationFactory

public class VMRunnerConfigurationFactory {

    public VMRunnerConfiguration create(final ILaunchConfiguration configuration, final IProgressMonitor monitor,
            final AbstractJavaLaunchConfigurationDelegate delegate) throws CoreException {
        final ExecutionArguments execArgs = new ExecutionArguments(delegate.getVMArguments(configuration), "");

            // Activator.MAIN_TYPE_NAME is the main class above
        final VMRunnerConfiguration runConfig = new VMRunnerConfiguration(Activator.MAIN_TYPE_NAME,
            delegate.getClasspath(configuration));
        runConfig.setBootClassPath(delegate.getBootpath(configuration));
        runConfig.setEnvironment(delegate.getEnvironment(configuration));
        runConfig.setProgramArguments(new ProgramArgumentsFactory().create(configuration, monitor));
        runConfig.setVMArguments(execArgs.getVMArgumentsArray());
        runConfig.setVMSpecificAttributesMap(delegate.getVMSpecificAttributesMap(configuration));
        runConfig.setWorkingDirectory(getWorkingDirectoryName(delegate.verifyWorkingDirectory(configuration)));
        return runConfig;
    }

    private String getWorkingDirectoryName(final File workingDir) throws CoreException {
        return workingDir != null ? workingDir.getAbsolutePath() : null;
    }

}

1 个答案:

答案 0 :(得分:2)

AbstractUIPlugin位于org.eclipse.ui.workbench插件中,因此您需要在依赖项中使用该插件。

编辑。

注意到你正在使用main方法 - 这是一个你无法运行的Eclipse插件,它是一个普通的Java程序。您必须在现有的Eclipse RCP中运行它或创建RCP。

编辑2

您的启动无法正确初始化Eclipse以运行插件。您必须通过org.eclipse.equinox.launcher.Main入口点(通常通过eclipse可执行文件)启动Eclipse应用程序。您必须指定要运行的应用程序或产品(例如Run Configuration中的Eclipse Application)。