<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>Service tracker java.lang.ClassCastException issue</title>
  <link rel="self" href="https://uat.liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=121192591" />
  <subtitle>Service tracker java.lang.ClassCastException issue</subtitle>
  <id>https://uat.liferay.dev/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=121192591</id>
  <updated>2026-10-15T09:46:51Z</updated>
  <dc:date>2026-10-15T09:46:51Z</dc:date>
  <entry>
    <title>RE: RE: Service tracker java.lang.ClassCastException issue</title>
    <link rel="alternate" href="https://uat.liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121193904" />
    <author>
      <name>Aniket Pathare</name>
    </author>
    <id>https://uat.liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121193904</id>
    <updated>2021-10-12T07:57:46Z</updated>
    <published>2021-10-12T07:57:44Z</published>
    <summary type="html">&lt;p&gt;Hi Olaf, &lt;/p&gt;
&lt;p&gt;Thanks for the answer. After changing the API from compile to
  compileOnly solved my problem.&lt;/p&gt;</summary>
    <dc:creator>Aniket Pathare</dc:creator>
    <dc:date>2021-10-12T07:57:44Z</dc:date>
  </entry>
  <entry>
    <title>RE: Service tracker java.lang.ClassCastException issue</title>
    <link rel="alternate" href="https://uat.liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121193462" />
    <author>
      <name>Olaf Kock</name>
    </author>
    <id>https://uat.liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121193462</id>
    <updated>2021-10-12T07:57:40Z</updated>
    <published>2021-10-12T06:44:22Z</published>
    <summary type="html">&lt;p&gt;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 &amp;quot;object AA&amp;quot;
  is a of a subclass of &amp;quot;Interface A loaded by classloader X&amp;quot;,
  but you're trying to cast to &amp;quot;Interface A loaded by classloader Y&amp;quot;.&lt;/p&gt;
&lt;p&gt;Unfortunately the error message omits the classloader part (well,
  here it's attempting to do so, but it's not very clear)&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;</summary>
    <dc:creator>Olaf Kock</dc:creator>
    <dc:date>2021-10-12T06:44:22Z</dc:date>
  </entry>
  <entry>
    <title>Service tracker java.lang.ClassCastException issue</title>
    <link rel="alternate" href="https://uat.liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121192590" />
    <author>
      <name>Aniket Pathare</name>
    </author>
    <id>https://uat.liferay.dev/c/message_boards/find_message?p_l_id=119785294&amp;messageId=121192590</id>
    <updated>2021-10-18T19:19:34Z</updated>
    <published>2021-10-11T13:23:49Z</published>
    <summary type="html">&lt;p&gt;Hi Guys,&lt;/p&gt;
&lt;p&gt;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 &lt;a
    href="https://help.liferay.com/hc/en-us/articles/360017889592-Service-Trackers"&gt;Liferay's
  documentation&lt;/a&gt; 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.&lt;/p&gt;
&lt;p&gt;
  &lt;br /&gt; 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)&lt;/p&gt;
&lt;p&gt;Below is the code snippet:&lt;/p&gt;
&lt;p&gt;Bundle bundle = FrameworkUtil.getBundle(this.getClass());&lt;br /&gt;   
   BundleContext bundleContext = bundle.getBundleContext();&lt;br /&gt;   
   ServiceTracker&amp;lt;IDMSService, IDMSService&amp;gt; serviceTracker =&lt;br /&gt;
          new ServiceTracker(bundleContext, IDMSService.class,
  null);&lt;br /&gt;     &lt;br /&gt;     @RenderMapping(params =
  &amp;quot;javax.portlet.action=success&amp;quot;)&lt;br /&gt;     public String
  showGreeting(ModelMap modelMap) throws InterruptedException{&lt;br /&gt;   
       serviceTracker.open();&lt;br /&gt;       
   if(!serviceTracker.isEmpty()) {&lt;br /&gt;           
   System.out.println(&amp;quot;:::Test Message:::
  &amp;quot;+serviceTracker.waitForService(500).testidmsService());&lt;br /&gt;   
       }else {&lt;br /&gt;           
   System.out.println(&amp;quot;:::::::::service not
  found:::::::::&amp;quot;);&lt;br /&gt;         }&lt;br /&gt;     }&lt;/p&gt;
&lt;p&gt;I am using Liferay DXP 7.3 sp2 bundle.&lt;/p&gt;</summary>
    <dc:creator>Aniket Pathare</dc:creator>
    <dc:date>2021-10-11T13:23:49Z</dc:date>
  </entry>
</feed>
