Class path issue: NoClassDefException :

855 Views Asked by At

Here's the issue, i have this one project X that uses another project Y's services. Y was exported as jar file using eclipse and added to the project X build path. it so happens that the class that the spring loads "classService" in X can't load because there's something wrong with initializing dependency in the contructor of the remoteService which is in Y's StringEncryptorService. it says something about the class within a jar in Y. there's no compilation error, so the build path is good. Thanks Ahead. Any Answer will be appreciated

INFO: Initializing Spring root WebApplicationContext
ERROR org.springframework.web.context.ContextLoader:319 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'classService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.package.common.security.StringEncryptorService com.package.service.classService.stringEncryptorService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'stringEncryptorService' defined in URL [jar:file:/C:/Users/user/git/project/Project/WEB-INF/lib/package-common-0.111.jar!/com/package/common/security/StringEncryptorService.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.package.common.security.StringEncryptorService]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64

I used commons-codec-1.8 which is in Y project. i know i can move it's jar file to Project X' referenced library, but i wonder if i can make it in Y so that when i make other project, say A, i can just export y as jar to A.

Edit: i think the issue is that the spring can't find the common-codec. since it is in under Project Y's library, the Project that i exported to Project X. can we do something so that the spring in Project X can scan through the library of Project Y? or specifically to the library of common-codec.jar. correct me if i'm wrong if i'm saying it wrong.

Answer: either add library explicitly to classpath of X by copying jar to it or add it as external jar to the project or add project to classpath in eclipse. since i want to let the library common-codec.jar to be always in Project Y library, we just added to application-context.xml of Project X:

<bean id="StringEncryptorService" class="PackagePathInProjectY.StringEncryptorService"></bean>

Special Thanks to: Harish Kumar. Thanks Buddy..


2

There are 2 best solutions below

1
On

org/apache/commons/codec/binary/Base64 is not found in your project, please add the commons-codec-1.8.jar in build path of X project ,another way is when you export the project Y remeber to add the commons-codec-1.8.jar in project Y together.

7
On

There is definitely an issue with classpath in your deployed project to tomcat. Can you check if WEB-INF/lib has this library? If you are deploying the project as war then while building war or project X both libraries should be bundled and when war explodes on tomcat then both Y library and commons codec should be found in class path. You can bundle commons codec from Porject Y as part of build.

Harish