Java NoClassDefFoundError ErrorCoded

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52395906/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:26:40  来源:igfitidea点击:

NoClassDefFoundError ErrorCoded

javaspringspring-bootjunit

提问by Eric Hansen

I am trying to run a JUnit test in my SpringBoot app but am getting the NoClassDefFoundError shown below.

我正在尝试在我的 SpringBoot 应用程序中运行 JUnit 测试,但出现如下所示的 NoClassDefFoundError。

Both the spring-boot-starter-parent and spring-boot-starter-test (v2.0.5.RELEASE) are in the pom.xml file. I added the spring-core (v5.0.9.RELEASE) in as well.

spring-boot-starter-parent 和 spring-boot-starter-test (v2.0.5.RELEASE) 都在 pom.xml 文件中。我也添加了 spring-core (v5.0.9.RELEASE)。

Apparantly, the ErrorCoded class has been deprecated as of 4.3.6, so I'm not sure how to find out why the test runner (or other library) is still trying to load this class.

显然,自 4.3.6 起,ErrorCoded 类已被弃用,所以我不确定如何找出测试运行程序(或其他库)仍在尝试加载此类的原因。

Here is my test class:

这是我的测试课:

@RunWith(SpringJUnit4ClassRunner.class)
public class SystemBuilderTest {

    private System system;

    @Before
    public void setUp() throws Exception {


        StatefulConnection conn = new StatefulConnection.Builder(null)
                .build();

        Device d1 = new SensingDevice.Builder("sensor1", conn)
                .build();

        system = new System.SystemBuilder("testSystem")
                .addChildDevice(d1)
                .build();

        system.initialize();

    }

    @Test
    public void testStart() throws DCFDeviceException {
        system.start();
        assertTrue(system.getName().equals("testSystem"));
        assertTrue(system.getChildDevices().size() == 1);
        assertTrue(system.getChildDevices().iterator().next().getName().equals("sensor1"));

    }
} 

... and the stack trace:

...和堆栈跟踪:

java.lang.NoClassDefFoundError: org/springframework/core/ErrorCoded
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:801)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:699)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:622)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:580)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    at java.base/java.lang.ClassLoader.defineClass1(Native Method)
    at java.base/java.lang.ClassLoader.defineClass(ClassLoader.java:1007)
    at java.base/java.security.SecureClassLoader.defineClass(SecureClassLoader.java:174)
    at java.base/jdk.internal.loader.BuiltinClassLoader.defineClass(BuiltinClassLoader.java:801)
    at java.base/jdk.internal.loader.BuiltinClassLoader.findClassOnClassPathOrNull(BuiltinClassLoader.java:699)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClassOrNull(BuiltinClassLoader.java:622)
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:580)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    at org.springframework.context.support.GenericApplicationContext.<init>(GenericApplicationContext.java:110)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:115)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:275)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:251)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:99)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:117)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access
at o.s.t.c.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
0(ParentRunner.java:58) at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206) Caused by: java.lang.ClassNotFoundException: org.springframework.core.ErrorCoded at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582) at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185) at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496) ... 50 common frames omitted

Thanks in advance for any suggestions...

在此先感谢您的任何建议...

采纳答案by Roddy of the Frozen Peas

Something in your build is pulling in a dependency on spring-test 4.3.x (not sure what minor release, but it shouldn't be relevant.)

您的构建中的某些内容依赖于 spring-test 4.3.x(不确定哪个次要版本,但它不应该相关。)

The reason I say this is because the line numbers in the stack trace do not align with the line numbers in the SpringJUnit4ClassRunner class in the 5.x releases.

我之所以这么说是因为堆栈跟踪中的行号与 5.x 版本中 SpringJUnit4ClassRunner 类中的行号不一致。

From your stack trace:

从您的堆栈跟踪:

mvn dependency:tree

That indicates that at line 227 there should be a call to a createTestmethod. You can see that this is true in the 4.3.x branch versionof this class.

这表明在第 227 行应该有一个对createTest方法的调用。可以看到在这个类的4.3.x分支版本中也是如此。

But in the 5.0.x branch version, line 227 is instead a call to getTestContextManager().prepareTestInstance(testInstance);.

但在5.0.x 分支版本中,第 227 行改为调用getTestContextManager().prepareTestInstance(testInstance);.

This is pretty conclusive. So if your IDE is telling you otherwise (as you indicated in another comment), it is wrong. Or you might need to do a 'clean' on your project, if the IDE supports that functionality.

这是非常确定的。因此,如果您的 IDE 以其他方式告诉您(正如您在另一条评论中指出的那样),那是错误的。或者,如果 IDE 支持该功能,您可能需要对项目进行“清理”。

Since you did not post your full pom it is not possible to tell you what is pulling in the old dependency. However, you should be able to figure it out by using the Maven dependency plugin:

由于您没有发布完整的 pom,因此无法告诉您旧依赖项中的内容。但是,您应该能够通过使用 Maven 依赖项插件来弄清楚:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SystemBuilderTest {

This command will print the dependency hierarchy in tree form. I recommend redirecting output to file and grepping for spring-test. That will allow you to identify the problematic dependency and use a maven <exclusion>to disallow the problematic dependency.

此命令将以树形式打印依赖关系层次结构。我建议将输出重定向到文件并 grepping 进行 spring-test。这将允许您识别有问题的依赖项并使用 maven<exclusion>禁止有问题的依赖项。

回答by trjade

I cannot answer this exactly why is it error but I can tell that you have the conflict in version of some dependency.

我无法确切回答为什么会出错,但我可以说您在某些依赖项的版本中存在冲突。

As you can see in the document below(although you use version 2.0.5.RELEASE).

正如您在下面的文档中看到的(尽管您使用的是 2.0.5.RELEASE 版本)。

https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/

https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/

ErrorCoded is used by SpringJUnit4ClassRunner but if you see in their Github, SpringJUnit4ClassRunner does not use ErrorCoded anymore in version 5.0.8 of org.springframework.spring-test.

SpringJUnit4ClassRunner 使用了 ErrorCoded,但如果您在他们的 Github 中看到,SpringJUnit4ClassRunner 在 org.springframework.spring-test 的 5.0.8 版中不再使用 ErrorCoded。

You have to see if your Spring project really uses which version of org.springframework.spring-test because SpringJUnit4ClassRunner is in that jar.

您必须查看您的 Spring 项目是否真的使用了 org.springframework.spring-test 的哪个版本,因为 SpringJUnit4ClassRunner 在那个 jar 中。

Hope this helps.

希望这可以帮助。

回答by kj007

Can you pleaser un your test with SpringRunner.classand @SpringBootTestwill load all required test dependencies.

你能取悦联合国与测试SpringRunner.class,并@SpringBootTest会加载所有需要测试的依赖。

λ mvn dependency:tree | grep test
[INFO] +- org.springframework.security:spring-security-test:jar:4.2.3.RELEASE:test
[INFO] |  \- org.springframework:spring-test:jar:4.3.9.RELEASE:test

回答by prayagupd

For me happened to be older version of spring-testpulled by the spring-security-test. I am using spring-boot 2.x.

对我来说正好是旧版本的弹簧试验由拉spring-security-test。我正在使用spring-boot 2.x

<properties>
    <java.version>1.8</java.version>
    <spring.version>2.0.4.RELEASE</spring.version>
    <logging.version>2.8.2</logging.version>
    <spring-security-test.version>5.0.6.RELEASE</spring-security-test.version>
    <spring-security-config.version>5.0.6.RELEASE</spring-security-config.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-core</artifactId>
        <version>${spring-security-config.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring-security-config.version}</version>
        <scope>compile</scope>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <version>${spring-security-test.version}</version>
        <scope>test</scope>
    </dependency>
</dependencies>

Updating spring-security and spring-security-test to 5.xfixed it.

更新 spring-security 和 spring-security-test 以5.x修复它。

##代码##