I have deployed EJB JAR on Wildfly server. When I try to invoke an EJB from a client I get an exception,
Exception in thread "main" java.lang.ClassCastException: org.wildfly.naming.client.store.RelativeContext cannot be cast to vwg.sample.ejb.HelloLocal
at vwg.sample.ejb.TestEjb.main(TestEjb.java:19)
EJB version: 3.0 Wildfly server version: 15
Below are my project details,
Local EJB interface:
package vwg.sample.ejb;
public interface HelloLocal {
public void sayHello();
}
Stateless bean class:
package vwg.sample.ejb;
import javax.ejb.Local;
import javax.ejb.Stateless;
@Stateless
@Local ({HelloLocal.class})
public class HelloBean implements HelloLocal{
public void ejbCreate() {
System.out.println("ejb - create");
}
public void sayHello() {
System.out.println("Hello World by EJB 2.x ...");
}
}
Library added in the build path of server: jboss-ejb-api_3.2_spec-1.0.1.Final.jar
Client class:
package vwg.sample.ejb_client;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import vwg.sample.ejb.HelloLocal;
public class TestEjb {
public static void main(String[] args) {
try {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.wildfly.naming.client.WildFlyInitialContextFactory");
props.put(Context.PROVIDER_URL, "remote+http://localhost:8080");
props.put("jboss.naming.client.ejb.context", true);
InitialContext ctx = new InitialContext(props);
//System.out.println(ctx.lookup("ejb:EjbWildflyTest/HelloBean!vwg.sample.ejb.HelloLocal"));
HelloLocal helloLocalBean = (HelloLocal) ctx.lookup("ejb:EjbWildflyTest/HelloBean!vwg.sample.ejb.HelloLocal");
//System.out.println(helloLocalBean);
helloLocalBean.sayHello();
} catch (NamingException e) {
e.printStackTrace();
}
}
}
Libraries in client project classpath are shown in attached image.