Spring throws NoClassDefFoundError: MethodInterceptor although class exists within classpath

一曲冷凌霜 提交于 2019-12-04 04:39:03

问题


I'm developing a simple, training application with Spring MVC and Hibernate. I'm using Maven as build tool. All dependencies (spring, hibernate, aopalliance, junit etc.) are resolved using Maven's pom.xml file.

$ mvn war:war glassfish:deploy works absolutley fine, the project is being deployed to the GlassFish server - all *.jar files are copied (including com.springsource.org.aopalliance-1.0.0.jar).

I've made a simple servlet to test whether aopalliance exisists within classpath:

protected void doGet(...) throws ... {
    response.getWriter().println(org.aopalliance.intercept.MethodInterceptor.class.getCanonicalName());
}

And it exists. The above code displays org.aopalliance.intercept.MethodInterceptor as expected.

However if I change the servlet into something like that:

protected void doGet(...) throws ... {
    response.getWriter().println(org.springframework.transaction.interceptor.TransactionInterceptor.class.getCanonicalName());
}

It throws an exception:

java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

TransactionInterceptor uses aopalliance interfaces, but I don't understand why it cannot find them, while my servlet can. I believe it might be somehow related to the classloader, but I'm affraid I have no idea how to fix it.

EDIT:

Some details:

  • Complete pom.xml file: http://pastebin.com/430iPgRs
  • Complete HelloServlet class: http://pastebin.com/YVfzz4i8
  • Exception full stack trace: http://pastebin.com/UZ5nAJdZ

EDIT:

I've also added dependencies for spring.osgi.core/io as suggested by @Ravi:

<dependency>
    <groupId>org.springframework.osgi</groupId>
    <artifactId>org.springframework.osgi.core</artifactId>
    <version>1.2.1</version>
</dependency>

<dependency>
    <groupId>org.springframework.osgi</groupId>
    <artifactId>org.springframework.osgi.io</artifactId>
    <version>1.2.1</version>
</dependency>

But it didn't fix the problem.

However, I've tried to run the very same application on VMware vFabric tc Server, which is delivered with SpringSource Tool Suite, and everything worked just fine. This seems to be GlassFish-specific issue.

I'm using GlassFish Server Open Source Edition 3.1.1.

Another strange thing: if I redeploy application (using "Publish" in Eclipse) the servlet throws:

java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor

But after refresh (whitin a browser) I get:

java.lang.NoClassDefFoundError: org/springframework/transaction/interceptor/TransactionInterceptor

Further refreshes don't change anything.


回答1:


You might have another and incomplete spring somewhere in parent classloader, most likely in {domaindir}/lib




回答2:


I had the same issue and after hunting for answers for almost a week, I found out that you need aopalliance.jar. This solved my problem.




回答3:


Have you included Spring transaction jars in your classpath. The source for TransactionInterceptor.java contains reference to some of the org.springframework.transaction and org.springframework.transaction.support packages.

In your first snippet org.aopalliance.intercept.MethodInterceptor.class.getCanonicalName() you are only loading the aopalliance class which has no dependencies on the Spring Transaction libraries.

In the second snippet you are loading TransactionInterceptor class and its dependencies (org.springframework.transaction.interceptor.TransactionInterceptor.class.getCanonicalName()). The second exception you see after browser refresh makes sense compared to previous error (before browser refresh)

The first snippet is a independent class but the second snippet is a Spring class load which is a wrapper over aopalliance. Spring is trying to load its own dependencies (transaction related classes and aopalliance implementation).

java.lang.NoClassDefFoundError is thrown when one of its dependencies is not found at runtime although its found at compile time (dependency issues)

Try adding these dependencies and check if it resolves.



来源:https://stackoverflow.com/questions/9154800/spring-throws-noclassdeffounderror-methodinterceptor-although-class-exists-with

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!