In JBoss application server, how do I make sure that all modules in a deployment use the same Classloader for a jar library?
I have a deployable ear that I'm deploying to a JBoss application server. I am in the process of refactoring my code so that my entity classes are declared in a separate jar library that can be shared by the different modules in the ear.
However, I am now getting the following error:
java.lang.ClassCastException: model.project.Project cannot be cast to model.EntityBase
EntityBase is a public abstract class. Project is a public class that extends EntityBase. Both classes have been moved to my new jar library. This error leads me to the conclusion that there is an issue with the classloader.
My ear is structured as follows:
myapp.ear | |--- webportal.war |--- restapi.war |--- dal.jar
My entities had been defined in the dal.jar which is used by the war libraries to access my database. I have now removed the entities from the dal.jar and placed them in a separate model.jar. I have listed the model.jar as a dependency in the pom.xml for myapp.ear. It is also listed as a dependency in the pom.xml files for webportal.war, restapi.war, and dal.jar. As a result the model.jar is now deployed in the lib directory within myapp.ear.
Is my understanding of the cause of the ClassCastException correct? If so, how do I package my application to ensure that all of the classes in model.jar are loaded with the same classloader?
It turned out to be a relatively simple solution. I modified the pom.xml files for webportal.war, restapi.war, and dal.jar by adding a scope tag with a value of provided for the model dependency:
<dependency> <groupId>myproject</groupId> <artifactiId>model</artifactId> <version>0.0.1-SNAPSHOT</version> <scope>provided</scope> </dependency>
I then modified the pom.xml for myapp.ear by adding a dependency to model.jar with no scope tag (this will give the dependency the default scope which I believe is compile):
<dependency> <groupId>myproject</groupId> <artifactiId>model</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>