0

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.

I have referred this documentation, but could not understand the problem with my classpath if there is any

Ahmed Sbai
  • 10,695
  • 9
  • 19
  • 38
Abhishek
  • 1
  • 2
  • Please remember that Stack Overflow is not your favourite Java(?) forum, but rather a question and answer site for all programming related questions. Thus, always include the tag of the language you are programming in, that way other users familiar with that language can more easily find your question. Take the [tour] and read up on [ask] to get more information on how this site works, then [edit] the question with the relevant tags. – Adriaan Mar 13 '23 at 11:16

0 Answers0