3

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:

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.

Crozin
  • 43,890
  • 13
  • 88
  • 135
  • may be you are hitting this [issue](https://jira.springsource.org/browse/SPR-6575) try upgrading to Spring 3.1.0.RELEASE – Ravi Kadaboina Feb 06 '12 at 04:42
  • @Ravi: I'am using 3.1.0.RELEASE. I've updated my question with some details. – Crozin Feb 06 '12 at 09:14
  • Did you try to investigate classloader paths under debugger? Looks like servlet's classloader does not have access to this class. I would put a breakpoint at doGet, when reached add an exception breakpoint for NoClassDefFoundError and investigate the classloader hierarchy paths, this could give you a hint. – mrembisz Feb 06 '12 at 09:43
  • Since Glassfish is OSGI, you may encounter an issue here. Although, have you tried to build the war, and look in the WEB-INF/lib for the missing classe ? – ndeverge Feb 06 '12 at 11:12
  • @mrembisz: `WebappClassLoader` object has `jarRealFiles` property (of type `File[]`) and I *can* find there `File(...\WEB-INF\lib\com.springsource.org.aopalliance-1.0.0.jar)` – Crozin Feb 06 '12 at 12:10
  • @nico_ekito: WAR archive contains `aopalliance.jar` and deployed application contains it as well. – Crozin Feb 06 '12 at 12:11
  • Actually your log shows exception "java.lang.NoClassDefFoundError: org/springframework/transaction/interceptor/TransactionInterceptor", not aop one. Is WebappClassLoader also classloader for `HelloServlet` class? – mrembisz Feb 06 '12 at 12:50
  • I think you might also need spring-osgi-core and spring-osgi-io dependencies added to your project. See this [link](http://www.springsource.org/osgi) – Ravi Kadaboina Feb 06 '12 at 16:37
  • @Ravi: Unfortunately it didn't help, but I've updated the question. – Crozin Feb 06 '12 at 22:26
  • If you think it's a GF issue trying using a 3.1.2 release candidate (full list) http://dlc.sun.com.edgesuite.net/glassfish/3.1.2/promoted/ or get the latest dlc.sun.com.edgesuite.net/glassfish/3.1.2/promoted/latest-glassfish.zip – Preston Feb 06 '12 at 23:49
  • @mrembisz: It seems that application throws two exceptions - see my last update to the question. – Crozin Feb 08 '12 at 14:49
  • @Preston: I don't think it's a bug... more like some invalid configuration of Glassgish. – Crozin Feb 08 '12 at 14:50

3 Answers3

7

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.

1

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

pdudits
  • 896
  • 6
  • 9
  • Can you please explain this answer in detail. I am facing an issue where MyWar uses MyJar as a dependency and MyWar's spring is not able to find MyJar's Class. It just says NoClassDefFound, when actually eclipse/maven is building it correct, and MyJar is there in (MyWar extracted)'s lib. – Dipanshu Verma Jun 17 '17 at 13:52
0

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.

Community
  • 1
  • 1
Rajendra
  • 1,703
  • 2
  • 16
  • 22
  • I've got JARes that contains: `org.aopalliance[.intercept]` and `org.springframework.transaction[.support]` packages at both, compile-time and runtime. I'm not sure whether I understood you correctly, but why Spring is trying to load its own dependencies while they're already available? Some ClassLoader issue? PS. The same project run on VMware vFabric tc Server runs just fine. – Crozin Feb 08 '12 at 14:43
  • That's annoying. If you are using maven to build it should resolve all the dependencies. Make sure you haven't set any dependency scope as "provided". NoClassDefFoundError suggests that there some jar that is missing in the dependency tree of "org.springframework.transaction.interceptor.TransactionInterceptor". All import statements in org.springframework.transaction.interceptor.TransactionInterceptor.java and their dependencies should be there in you glassfish webapp server. As it works with vFabric it surely is with missing dependencies. Can you try deploying in Tomcat and check if it works? – Rajendra Feb 08 '12 at 16:00