EMM EMM - 7 months ago 41
Java Question

Spring: nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

Stack trace:

Oct 24, 2014 8:12:04 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@50df2e: startup date [Fri Oct 24 08:12:04 IST 2014]; root of context hierarchy
Oct 24, 2014 8:12:05 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [knights-aop.xml]
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from class path resource [knights-aop.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:412)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:174)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:209)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:180)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:243)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:127)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:93)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:537)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:451)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.java.spring.SpringDemo.main(SpringDemo.java:12)
Caused by: java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice
at org.springframework.aop.config.ConfigBeanDefinitionParser.getAdviceClass(ConfigBeanDefinitionParser.java:410)
at org.springframework.aop.config.ConfigBeanDefinitionParser.createAdviceDefinition(ConfigBeanDefinitionParser.java:366)
at org.springframework.aop.config.ConfigBeanDefinitionParser.parseAdvice(ConfigBeanDefinitionParser.java:332)
at org.springframework.aop.config.ConfigBeanDefinitionParser.parseAspect(ConfigBeanDefinitionParser.java:227)
at org.springframework.aop.config.ConfigBeanDefinitionParser.parse(ConfigBeanDefinitionParser.java:115)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.parse(NamespaceHandlerSupport.java:73)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1438)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.parseCustomElement(BeanDefinitionParserDelegate.java:1428)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:195)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:139)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:108)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:493)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:390)
... 14 more


config xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

<bean id="knight" class="com.java.spring.BraveKnight">
<constructor-arg ref="quest" />
</bean>

<bean id="quest" class="com.java.spring.ResqueDamselQuest" />

<bean id="minstrel"
class="com.java.spring.Minstrel" /> <!--<co id="co_minstrel_bean"/>-->

<aop:config>
<aop:aspect ref="minstrel">

<aop:pointcut id="embark"
expression="execution(* *.embarkOnQuest(..))" /> <!--<co id="co_define_pointcut"/>-->

<aop:before pointcut-ref="embark"
method="singBeforeQuest"/> <!--<co id="co_minstrel_before_advice"/>-->

<aop:after pointcut-ref="embark"
method="singAfterQuest"/> <!--<co id="co_minstrel_after_advice"/>-->

</aop:aspect>
</aop:config>
</beans>


My main class:

public class SpringDemo {
public static void main(String[] args) {
final ApplicationContext applicationContext = new ClassPathXmlApplicationContext("knights-aop.xml");
final Knight braveKnight = (Knight) applicationContext.getBean("knight");
braveKnight.embarkOnQuest();
}
}


I also tried adding: aopalliance.jar & aopalliance-alpha1.jar individually and together but the error won't go away.

There are similar questions here on SO: spring nested exception is java.lang.NoClassDefFoundError: org/aopalliance/aop/Advice

###########EDIT###########Update: Other classes in the project##

Minstrel.java

package com.java.spring;

public class Minstrel {
public void singBeforeQuest(){
System.out.println("Fa la la Theknight is so brave!");
}
public void singAfterQuest(){
System.out.println(
"Tee hee he The brave knight did embark on a quest!");
}
}


BraveKnight.java

package com.java.spring;

public class BraveKnight implements Knight {
private Quest quest;

BraveKnight(Quest quest) {
this.quest = quest;
}

@Override
public void embarkOnQuest() {
quest.embark();
}
}


RescueDamselQuest.java

package com.java.spring;

public class RescueDamselQuest implements Quest {

@Override
public void embark() {
System.out.println("Damsel rescue quest is on.");
}
}



I am using Spring 3.2

##################EDIT2##################
I am not using Maven. Please don't provide Maven related solution unless Spring AOP is impossible without it.

Project Snap:
enter image description here

Inside SPRING LIBRARY I have all the jar that got downloaded with Spring. Is it possible that some spring specific jar conflicting with aopalliance jar?


If anyone has some similar & simple working example without Maven, please provide it.


Thanks
~Mohit

EMM EMM
Answer

I removed SPRING-LIBRARY completely and then added all the required jars one by one plus cglib-nodep-2.2.jar.
There was also a spelling mistake in my code <bean id="quest" class="com.java.spring.ResqueDamselQuest" />

public class RescueDamselQuest implements Quest {

Name of class.

But I am sure this spelling mistake was not the reason for the exception I was getting. After adding the jars from scratch I got this error that said the bean is not found as mentioned in the knights-aop.xml sth.. sth... which is when I figured that I have this typo.

This is how my jars are added now:

enter image description here

I hope someone might get help from this answer.

###########EDIT###########

asm-all & cglib-nodep are not required. Also, some jar from spring download was causing problems, as I tried adding all the jars I got in spring download and it started showing the old exception again. So it is better to add jar as and when required.