exception":"java.lang.ClassCastException in liferay service builder

1.5k views Asked by At

I have worked on implementation of the Entity remote service. I have created one custom service method in EntityServiceImpl,Created custom service method providing service through InstitutionServiceUtil. After deploy the portlet, while sending request to service method through from browser window,I am getting below Exception

exception":"java.lang.ClassCastException: com.institutions.model.impl.InstitutionImpl 
           cannot be cast to com.institutions.model.Institution

Note: If I send the request after restart the server, I didn't get above exception.

How to resolve the above Exception?

2

There are 2 answers

0
Aaron Digulla On

I assuming that InstitutionImpl implements the interface Institution. If so, then the root cause of the exception is classloading: Classloader A did load Institution but InstitutionImpl was loaded from a different classloader. Two classes in Java are only equivalent if the fully qualified name and the classloader are the same.

I don't know enough about liferay to tell you how it's class loading works. But to solve the problem, you need to find out if the Institution interface could already be around when you try to load your implementation (maybe from a previous deployment attempt).

0
Navankur Chauhan On

While deploying the portlets that throws the class cast exception, do the following:

  1. deploy the application in the liferay/deploy.
  2. shutdown the liferay
  3. move the service jar from the WEB-INF/lib from the portlet to the /lib/ext of the tomcat
  4. remove the temp and work folder from the tomcat
  5. restart the tomcat.

OR ...what worked for me was

  1. change the package name while building the service.xml in the service.xml file

Or if you have already built the service, do these steps

  1. Just delete the 5 packages that are created from the service builder, i.e

    model.impl

    service.base

    service.http

    service.impl

    service.persistence

  2. delete the .xml generated in the META-INF folder except for the file ext-spring.xml

  3. delete the XX-service.jar from the docroot/lib folder
  4. delete the service folder in the docroot folder.
  5. change the package name in the service.xml and build the path.