Service tracker java.lang.ClassCastException issueService tracker java.lang.ClassCastException issuehttps://liferay.dev/en/c/message_boards/find_thread?p_l_id=119785333&threadId=1211925912024-03-28T14:54:24Z2024-03-28T14:54:24ZRE: RE: Service tracker java.lang.ClassCastException issueAniket Patharehttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1211939042021-10-12T07:57:46Z2021-10-12T07:57:44Z<p>Hi Olaf, </p>
<p>Thanks for the answer. After changing the API from compile to
compileOnly solved my problem.</p>Aniket Pathare2021-10-12T07:57:44ZRE: Service tracker java.lang.ClassCastException issueOlaf Kockhttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1211934622021-10-12T07:57:40Z2021-10-12T06:44:22Z<p>Whenever you can't typecast an object to a legitimate superclass or
interface, you're dealing with multiple instances of the
superclass/interface on the classpath. So, your "object AA"
is a of a subclass of "Interface A loaded by classloader X",
but you're trying to cast to "Interface A loaded by classloader Y".</p>
<p>Unfortunately the error message omits the classloader part (well,
here it's attempting to do so, but it's not very clear)</p>
<p>The solution? Make extra extra extra sure that you have only one
instance of the interface in your runtime. E.g. never compileInclude
an API, never copy interfaces or classes - especially exported ones -
into other projects.</p>Olaf Kock2021-10-12T06:44:22ZService tracker java.lang.ClassCastException issueAniket Patharehttps://liferay.dev/en/c/message_boards/find_message?p_l_id=119785333&messageId=1211925902021-10-18T19:19:34Z2021-10-11T13:23:49Z<p>Hi Guys,</p>
<p>I Have created some custom OSGi API and Services using API and
Service template on LDS. When I inject the service in OSGi liferay MVC
module is working properly. But when I am trying to access it in
Spring MVC portlet it is not working. As per <a
href="https://help.liferay.com/hc/en-us/articles/360017889592-Service-Trackers">Liferay's
documentation</a> we can use service tracker for accessing OSGi
service in Spring MVC portlets. But when I try to inject Service using
service tracker I am getting below exception.</p>
<p>
<br /> java.lang.ClassCastException: class
com.se.theexchange.commons.services.impl.IDMSServiceImpl cannot be
cast to class com.se.theexchange.commons.services.api.IDMSService
(com.se.theexchange.commons.services.impl.IDMSServiceImpl is in
unnamed module of loader
org.eclipse.osgi.internal.loader.EquinoxClassLoader @7d61d943;
com.se.theexchange.commons.services.api.IDMSService is in unnamed
module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @308c744b)</p>
<p>Below is the code snippet:</p>
<p>Bundle bundle = FrameworkUtil.getBundle(this.getClass());<br />
BundleContext bundleContext = bundle.getBundleContext();<br />
ServiceTracker<IDMSService, IDMSService> serviceTracker =<br />
new ServiceTracker(bundleContext, IDMSService.class,
null);<br /> <br /> @RenderMapping(params =
"javax.portlet.action=success")<br /> public String
showGreeting(ModelMap modelMap) throws InterruptedException{<br />
serviceTracker.open();<br />
if(!serviceTracker.isEmpty()) {<br />
System.out.println(":::Test Message:::
"+serviceTracker.waitForService(500).testidmsService());<br />
}else {<br />
System.out.println(":::::::::service not
fou