Search code examples
javamavenjakarta-eeejbwildfly-8

Class cast exception in Ejb lookup


I have a simple multimodule maven project with a war ejb and ear

When i am trying to access my ejb from a java pojo class using lookup it gives me class cast exception on wildfly it runs perfectly on jboss 7..

I have checked almost all the links related to this post please do help if anyone know how to resolve this..Here is my sample code:

My ejb interface:

package interfacejar;

import javax.ejb.Local;


public interface HelloWorldRemote {
       public String sayHello();       
       public void helloWait() ;  
}

Session Bean

@Stateless
public class HelloWorld implements Serializable, HelloWorldRemote {

    public String sayHello() {
        // TODO Auto-generated method stub
        return "hello";
    }

    public void helloWait(){
        // TODO Auto-generated method stub
    System.out.println("in ejb");   
    }



}  

My War Interface: //As i am accessing ejb from pojo class which is in different module of my maven project so it requires to create interface here also in this module

package interfacejar;

public interface HelloWorldRemote {
       public String sayHello();  

       public void helloWait() throws Exception;  
}

My Java class

public class Testnew{




    public HelloWorldRemote getProps(){
        HelloWorldRemote ref=null;
         try {
        Properties props = new Properties();
        props.setProperty("java.naming.factory.initial","org.jboss.as.naming.InitialContextFactory");
        props.setProperty("java.naming.provider.url", "localhost");
        props.setProperty("jboss.naming.client.ejb.context", "true");
        props.setProperty("java.naming.factory.url.pkgs", "org.jboss.ejb.client.naming");



            Context context = new InitialContext(props);

            Object obj =    context.lookup("java:global/MyEarFile/testejb-0.0.1-SNAPSHOT/HelloWorld!interfacejar.HelloWorldRemote");

             ref=(HelloWorldRemote) obj;

        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
         return ref;

    }




        public void getRemote(){
            HelloWorldRemote ref=null;
            try {
                ref=getProps();

                ref.helloWait();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
            }

}

Servlet that calls this class

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    this.processRequest(request, response);

}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    this.processRequest(request, response);
}
//java:global/MyEarFile/testejb-0.0.1-SNAPSHOT/HelloWorld!interfacejar.HelloWorldRemote*
/*@Resource(mappedName = "java:global/MyEarFile/tstwar-0.0.1-SNAPSHOT/HelloWorld!interfacejar.HelloWorldRemote")
private HelloWorldRemote helloWorldRemote;*/

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("in servlet");

    Testnew testnew = new Testnew();
    testnew.getRemote();


}

This is the error that i got

21:06:49,205 ERROR [stderr] (default task-3) java.lang.ClassCastException: interfacejar.HelloWorldRemote$$$view4 cannot be cast to interfacejar.HelloWorldRemote

21:06:49,205 ERROR [stderr] (default task-3)    at com.test.Testnew.getProps(Testnew.java:43)

21:06:49,205 ERROR [stderr] (default task-3)    at com.test.Testnew.getRemote(Testnew.java:59)

Solution

  • I found the solution

    It did worked when i changed the lookup from ejb to war

    Context context = new InitialContext(props);
    
            Object obj =    context.lookup("java:global/MyEarFile/AllWar-0.0.1-SNAPSHOT/HelloWorld!interfacejar.HelloWorldRemote");
    
             ref=(HelloWorldRemote) obj;
    

    Giving the name of the war in which the lookup is being done solved my problem... Hope it will help others too