2 Replies Latest reply on Apr 21, 2005 5:25 AM by jjmargon

    ClassCastException when obtaining a Remote Interface from th

      Hi.
      First of all, I'm using JBoss 4.0.1 and Eclipse 3.0.1M6 with MyEclipse.
      I've deployed a Stateless Session Bean in the server and it's ok. In fact, when I see the jmx-console in the JNDI view I can see the values for the EJB deployed.
      The issue is that when I try to access the EJB through a client (in my case a JUnit Test Case class, but also I've tried with a standalone application), always I get a ClassCastException.
      The method of the client code I'm calling is:

      public void testClientEJB(){
       Properties props = new Properties();
       props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
       props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
       props.put(Context.PROVIDER_URL, "localhost:3332");
       try{
       InitialContext ctx = new InitialContext(props);
       assertTrue(ctx!=null);
       Object ref = ctx.lookup("ejb/HelloWorld");
       System.out.println("Got a reference to the EJB");
       HelloWorldHome pruebaHome = (HelloWorldHome) PortableRemoteObject.narrow(ref,HelloWorldHome.class);
       assertTrue(pruebaHome!=null);
       HelloWorld pruebaServer = (HelloWorld) pruebaHome.create();
       System.out.println(pruebaServer.execute());
       }catch (Exception e){
       e.printStackTrace();
       }
      }
      

      And the console output is:
      Got a reference to the EJB
      java.lang.ClassCastException
       at client.HelloWorldClientTest.testClientEJB(HelloWorldClientTest.java:29)
       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
       at java.lang.reflect.Method.invoke(Method.java:324)
       at junit.framework.TestCase.runTest(TestCase.java:154)
       at junit.framework.TestCase.runBare(TestCase.java:127)
       at junit.framework.TestResult$1.protect(TestResult.java:106)
       at junit.framework.TestResult.runProtected(TestResult.java:124)
       at junit.framework.TestResult.run(TestResult.java:109)
       at junit.framework.TestCase.run(TestCase.java:118)
       at junit.framework.TestSuite.runTest(TestSuite.java:208)
       at junit.framework.TestSuite.run(TestSuite.java:203)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
       at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
      

      Note: The line 29 in testClient is when I try to obtain the Remote Interface from the create method of the home interface,i.e.,
      HelloWorld pruebaServer = (HelloWorld) pruebaHome.create();
      


      Of course, I have in the classpath client the jbossall-client.jar
      I've had the Eclipse EJB Project linked as a Project required on the build path (to make the interfaces classes available to the application) and the error was there.
      I've tried to export a jar with the classes of the EJB and include this jar in the classpath of the application and the error continued there.

      Someone has experienced this kind of error?

      Thanks a lot

        • 1. Re: ClassCastException when obtaining a Remote Interface fro
          darranl

          There should not be a need to cast the object returned from the create method.

          The create method of the home interface should have a return type of the remote interface already. If the home interface is a local home the return type should be the local interface.

          What does your home interface look like?

          • 2. Re: ClassCastException when obtaining a Remote Interface fro

            Hi, again.
            The interfaces are remote (I don't run the junit client inside JBoss,but in a different virtual machine).

            But, don't worry. It works fine now!!!
            The error was mine.
            The issue was that the Remote Interface name (HelloWorld) and the bean class had the same name, and when the Eclipse shows the different classes to fix the import errors, I chose unintentionally the bean class instead the interface class.
            I've changed the class and it works.

            But thanks a lot for your attention and your time.

            Bye