title: Problem with JNDI look-up in WildFly 17: java.lang.ClassCastException: class is in unnamed module of loader
body: <p>I use Wildfly 17 and have a couple of stateless EJBs, one of which causes me sometimes ( but not always) problems upon JNDI lookup. This is my EJB:</p>
  
  <pre><code>@Stateless
  public class AVeDBService {
    //other stuff here
  }
  </code></pre>
  
  <p>When I deploy my application I see the following log for the above service:</p>
  
  <pre><code>java:global/heimdi/AVeDBService!at.home.digest.services.AVeDBService
  java:app/heimdi/AVeDBService!at.home.digest.services.AVeDBService
  java:module/AVeDBService!at.home.digest.services.AVeDBService
  ejb:/heimdi/AVeDBService!at.home.digest.services.AVeDBService
  java:global/heimdi/AVeDBService
  java:app/heimdi/AVeDBService
  java:module/AVeDBService
  </code></pre>
  
  <p>I have a ServiceLocator class that is a singleton looks up a stub of the above service using the following JNDI name:</p>
  
  <pre><code>    public class ServiceLocator {
  
       private static final String AVE_DB_SERVICE_JNDI_KEY = 
       "java:global/heimdi/AVeDBService!at.home.digest.services.AVeDBService";
  
       private Map&lt;String, Object&gt; serviceHolder = new HashMap&lt;String, Object&gt;();
  
       private ServiceLocator() {
          try {
              Context ctx = new InitialContext();
              AVeDBService aveDBService = (AVeDBService) ctx.lookup(AVE_DB_SERVICE_JNDI_KEY);
              serviceHolder.put(AVE_DB_SERVICE, aveDBService);
            }
  
       }
  
  }
  </code></pre>
  
  <p>Now, when I use the Service Locator to obtain a stub to my service, I sometimes get the exception below. But only sometimes:</p>
  
  <pre><code>Caused by: java.lang.ClassCastException: class at.home.digest.services.AVeDBService$$$view39 cannot be cast to class at.home.digest.services.AVeDBService (at.home.digest.services.AVeDBService$$$view39 is in unnamed module of loader 'deployment.heimdi.war' @658d7dca; at.home.digest.services.AVeDBService is in unnamed module of loader 'deployment.heimdi.war' @15cdf490)
  </code></pre>
  
  <p>I am using my service in the same war. module, in which it is declared (the same web-application).  But nevertheless the global lookup should always work, or? Could that be a bug in WildFly 17.0? I could not find an answer in similar related questions, such as:</p>
  
  <p><a href="https://stackoverflow.com/questions/25974302/portable-jndi-syntax-lookup-for-ejb-on-glassfish">Portable JNDI Syntax lookup for EJB on Glassfish</a></p>
  
  <p><a href="https://stackoverflow.com/questions/29039353/what-is-the-difference-between-jndi-binding-of-module-and-app-in-java-ee-6-7">what is the difference between jndi binding of module and app in Java ee 6/7?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/54536895/cannot-be-cast-to-class-because-they-are-in-unnamed-module-of-loader-app">Cannot be cast to class because they are in unnamed module of loader &#39;app&#39;</a></p>
  
  <p>Thank you!</p>