Message Boards

ClassCastException for using KBArticleLocalServiceUtil

thumbnail
Nirav Prajapati, modified 3 Years ago.

ClassCastException for using KBArticleLocalServiceUtil

Regular Member Posts: 133 Join Date: 6/25/15 Recent Posts

Hi Friends,

 

I am using Liferay 7.2

I have used KBArticle into XXXLocalServiceImpl using the below code.

KBArticle kbArticle = KBArticleLocalServiceUtil.getKBArticle(12345, WorkflowConstants.STATUS_ANY);

But it's throwing an exception as,

java.lang.ClassCastException: com.sun.proxy.$Proxy595 cannot be cast to com.liferay.knowledge.base.service.KBArticleLocalService

Instead of KBArticleLocalServiceUtil, I have used the below code, but again getting the same exceptions.

    @Reference
    private KBArticleLocalService _KbArticleLocalService;

    KBArticle kbArticle = _KbArticleLocalService.getKBArticle(12345, WorkflowConstants.STATUS_ANY);

 

Please help me to resolve the issue.

 

Thanks in Advance

thumbnail
Christoph Rabel, modified 3 Years ago.

RE: ClassCastException for using KBArticleLocalServiceUtil

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts

Did you per chance include the service into your module?

e.g. in build.gradle by using compileInclude? The error means that there are two versions of the service available. Maybe check the deploy folder, delete the osgi/state folder and especially, check your module.

thumbnail
Andrew Jardine, modified 3 Years ago.

RE: RE: ClassCastException for using KBArticleLocalServiceUtil

Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts

Another one I think that causes this is, that has bitten me anyway, is when I have accidentally deployed the same module under two different names. Let's say for example I decided that I wanted to change the bundle symbolic name in the bnd. I can now deploy "a new" module but the contents of the module are the same as the old one. Both can be in the osgi/modules side by side with the same classes using the same canonical names. 

Any chance that you have done that by accident?

thumbnail
Nirav Prajapati, modified 3 Years ago.

RE: ClassCastException for using KBArticleLocalServiceUtil

Regular Member Posts: 133 Join Date: 6/25/15 Recent Posts

Hi Chris,

Yes, I have added compile dependency in build.gradle. I have not mentioned any specific version so it has taken automatically.

    compile group: 'com.liferay', name: 'com.liferay.knowledge.base.api'
    compile group: 'com.liferay', name: 'com.liferay.knowledge.base.service'

Even the same code and dependency are working with a different module (portlet).

But getting exceptions while using KBArticle in service builder only.

Multiple times I did clean restart the server with no luck...

thumbnail
Christoph Rabel, modified 3 Years ago.

RE: ClassCastException for using KBArticleLocalServiceUtil

Liferay Legend Posts: 1554 Join Date: 9/24/09 Recent Posts

Can you look inside the generated jar file of your module? To make sure, only your files are in there and no Liferay classes?

Maybe it helps to change the dependency to compileOnly (instead of compile).

Also, please check the osgi/* folders, maybe there are two versions of the knowledge base jar file deployed. e.g. one in the marketplace, one in the modules folder.

thumbnail
Andrew Jardine, modified 3 Years ago.

RE: ClassCastException for using KBArticleLocalServiceUtil

Liferay Legend Posts: 2416 Join Date: 12/22/10 Recent Posts

Just want to throw out a long shot here. I had a similar issue a few weeks back. In my build.gradle file I was using the new release.portal.api dependency. When I was importging my models I wasn't paying attention and I imported the model from the Headles API instead of the model from the service API.

 

Again, a long shot, but one more thing to cross off the list. Can you double check to make sure that your KBArticle is referencing the correct class?