java.lang.NoClassDefFoundError for my DAO - java

i am using spring.xml for my controller and dao configuration. my spring-config.xml is as below:
<bean id="courseDao" class="com.platysgroup.lmex.adapter.moodle.dao.CourseDao"
init-method="setMoodleDataSource" depends-on="moodleAuthenticationDetails">
<property name="adapterDataSource" ref="adapterDataSource"></property>
<property name="userDao" ref="userDao"></property>
<property name="announcementDao" ref="announcementDao"></property>
<property name="roleDao" ref="roleDao"></property>
<property name="logDao" ref="logDao"></property>
</bean>
i have a coursedao class in my project in the package com.platysgroup.lmex.adapter.moodle.dao in the project name lmex-impl. but my spring config is not getting the class from a lmex-impl.jar. the spring-config.xml is on lmex-web project. and other thing is that he is showing me a wrong path in exception. the wrong name is Lcom/platysgroup/lmex/adapter/moodle/dao/CourseDao; instead of com/platysgroup/lmex/adapter/moodle/dao/CourseDao; the exception full stacktrace is as below:
javax.servlet.ServletException: Servlet.init() for servlet Spring MVC Dispatcher Servlet threw exception
org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)
org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)
org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)
org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)
root cause
java.lang.NoClassDefFoundError: Lcom/platysgroup/lmex/adapter/moodle/dao/CourseDao;
java.lang.Class.getDeclaredFields0(Native Method)
java.lang.Class.privateGetDeclaredFields(Class.java:2291)
java.lang.Class.getDeclaredFields(Class.java:1743)
org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.findResourceMetadata(CommonAnnotationBeanPostProcessor.java:315)
org.springframework.context.annotation.CommonAnnotation.........
java.lang.ClassNotFoundException: com.platysgroup.lmex.adapter.moodle.dao.CourseDao
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
please help me to resolve this.
Thank you

A NoClassDefFoundError is thrown when the JRE can't find a class. In your case, it can't find the class Lcom/platysgroup/lmex/adapter/moodle/dao/CourseDao, which you most probably did not add to your classpath.

The prefix 'L' indicates the instance of class. Please see Field Descriptors. You should focus on finding issue with configuration of bean courseDao and check that its all dependencies are there in same application context.

Related

springframework/remoting class not found

I am trying to setup JAX-WS client using spring. I have the below bean definition. Getting the error below.
<bean id="someClient" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="serviceInterface" value="somePortType" />
<property name="wsdlDocumentUrl" value="file:///someWSDL.wsdl"/>
<property name="namespaceUri" value="soemNS" />
<property name="serviceName" value="Services201006" />
</bean>
Caused by: org.springframework.beans.factory.CannotLoadBeanClassException: Error loading class [org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean] for bean with name ‘someClient' defined in ServletContext resource [/WEB-INF/jax-ws.xml]: problem with class file or dependent class; nested exception is java.lang.NoClassDefFoundError: Failed to link org/springframework/remoting/jaxws/JaxWsPortProxyFactoryBean (Module "deployment.JbossEAP_WS_WebServiceEAR.ear.JBossEap_WS_SpringService.war:main" from Service Module Loader): Failed to link org/springframework/remoting/jaxws/JaxWsPortClientInterceptor (Module "deployment.JbossEAP_WS_WebServiceEAR.ear.JBossEap_WS_SpringService.war:main" from Service Module Loader): org/aopalliance/intercept/MethodInterceptor
which version of spring are you using? It seems like a dependency is missing in the war file. The JaxWsPortClientInterceptor is part of spring-web.jar

Spring MVC project cannot use Hibernate

I want to build a Spring MVC project with Hibernate.
The IDE I used is Eclipse 4.4.2(Luna) and I installed the plugin Spring Tool Suite (STS) for Eclipse Luna (4.4).
The project I create is Spring project > Spring MVC Project.
Here is the External Jars I add :
antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.5.Final.jar
hibernate-core-4.3.9.Final.jar
hibernate-jpa-2.1-api-1.0.0.Final.jar
jandex-1.1.0.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.3.GA.jar
jboss-logging-annotations-1.2.0.Beta1.jar
jboss-transaction-api_1.2_spec-1.0.0.Final.jar
mysql-connector-java-5.0.8-bin.jar
Then, I add the detail of my database into /src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml :
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName" value="com.mysql.jdbc.Driver" />
<beans:property name="url" value="jdbc:mysql://192.168.1.43/MyDatabase" />
<beans:property name="username" value="testUser" />
<beans:property name="password" value="thePassOfTheUser" />
</beans:bean>
However, it shows the error message :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in ServletContext resource [/WEB-INF/spring/appServlet/servlet-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.jdbc.datasource.DriverManagerDataSource]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
org.springframework.jdbc.datasource.AbstractDataSource.<init>(AbstractDataSource.java:37)
org.springframework.jdbc.datasource.DriverManagerDataSource.<init>(DriverManagerDataSource.java:87)
Since I cannot fix it, I tried write hibernate.cfg.xml, but I have no idea where the file should be put.
I want to know how to set the bean to let my project can use Hibernate and how to use transitional way of hibernate (hibernate.cfg.xml) in the MVC project.
May 19th added
I found the solution.
It is because maven didn't load hibernate. Therefore after adding the following code into pom.xml it can work:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager‌​</artifactId>
<version>3.6.0.Final</version>
</dependency>
<dependency>
<groupId>org.‌​hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.0.Final</v‌​ersion>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</‌​artifactId>
<version>1.3.156</version>
</dependency>
<dependency>
<groupId>org.spring‌​framework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.6.RELEASE</ve‌​rsion>
</dependency>
Your error is related to missing library in classpath. Which is:
org.apache.commons.logging
Try to fix that first then tell if problem still occurs.
The stacktrace says that you're missing LogFactory which is part of Apache Commons Logging. I don't see it included in your external jars.
For the transitional way of hibernate create hibernate.cfg.xml configuration file and place it in the root of your application's classpath.
The XML configuration file must conform to the Hibernate 3 Configuration DTD, which is available from http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd.
Here is a tutorial on it.

Spring 4 config problems afer migration from v3 (util:properties location is no longer evaluating spEL)

I had a working spring 3 application which after migration to 4.1.0.RELEASE started to cause problems. Configuration below stopped working:
<jee:jndi-lookup id="dit_properties_path" jndi-name="dit_properties_path" resource-ref="true" />
<util:properties id="systemProperties" location="classpath:system.properties" />
<util:properties id="serverProperties" location="#{dit_properties_path}"/>
After migration it seems that spring is not able to resolve spEL #{dit_properties_path}, and is showing following error:
[PropertiesFactoryBean] [INFO]: Loading properties file from ServletContext resource [/#{dit_properties_path}]
[...]is java.io.FileNotFoundException: Could not open ServletContext resource [/#{dit_properties_path}]
Jndi resource is of type java.lang.String and points to local FS (C:\someFile.properties).
Here's tomcat definition of the resource /server and context/:
<Environment name="dit_properties_path" value="file:C:\someFile.properties"
type="java.lang.String" override="true"/>
and
<ResourceLink name="dit_properties_path"
type="java.lang.String"
global="dit_properties_path" />
Do anyone have any idea how to resolve this issue?
I have several property files in my app which are often accessed by #{propertyFileId['key']} and they have to be loaded dynamically from jndi resource path.
Change from :
<util:properties id="serverProperties" location="#{dit_properties_path}"/>
to:
<util:properties id="serverProperties" location="${dit_properties_path}"/>
solved the problem. Still, can't figure out why given expression doesn't work with hash sign.

Spring JDBC : unable to get dataSource

I have a spring XML config file called appConfig.xml which contains a datasource bean and another JDBCtemplate to which datasource is passed as an argument:
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
</bean>
<bean id="JDBCTemplate" class="com.myprojects.JDBCTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
I get an error :
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'JDBCTemplate' defined in file [appConfig.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: Property 'dataSource' is required
From what I see from this error, it is not able to pass dataSource to JDBCTemplate. The logs also says:
Loaded JDBC driver: com.mysql.jdbc.Driver
I have all dependencies mentioned in pom.xml and I verified that necessary jars for spring are loaded and mysql jdbc connector is also loaded. Any clues on what the issue may be?
I tried a different project where I manually added all spring dependencies and mysql jdbc connector as part of library. It worked fine there. But while trying to include dependencies via pom.xml, I'm facing this issue. So I'm assuming this is to do with some dependency not being pulled in or something. But unable to figure out which one from the error.
It thinks your class JDBCTemplate does not have a property named dataSource
Perhaps you don't have a public method setDataSource() that has a single argument of the right type.
Perhaps it is private.
Perhaps it is spelled wrong.

Some objects set by #Autowired always null, others are OK

I have been working on my first Spring project, and I've come across an annoying problem.
I have a class called 'UsernameService' which is configured as a bean in the dispatcher-servlet.xml:
<bean id="usernameService" class="service.UsernameService" scope="session" >
<aop:scoped-proxy />
</bean>
and when this bean is created in one of my classes (bean definition:)
<bean id="testController" class="controller.TestController" />
as such:
#Autowired
UsernameService uns;
it works absolutely fine. However, when I try and do the same in another class, LogController:
<bean id="logController" class="controller.LogController" />
then it does not work, and I get the following error:
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/flexitime] threw exception [Request processing failed; nested exception is java.lang.NullPointerException] with root cause
java.lang.NullPointerException
I've managed to (I believe) ascribe this error to the fact that uns is never actually set/created and remains as null inside LogController.
I have Googled this extensively and have found many 'solutions', however as yet none of them have worked.
Thanks!
James
Add the auto-wire attribute to your bean:
<bean id="usernameService" class="service.UsernameService" scope="session" autowire="byName">
<aop:scoped-proxy />
</bean>

Categories

Resources