0

At first I want to tell that similar type of questions has been asked here but still I can't find the solution of my problem.

I have a EJB 3 Project (name = HelloWorldEJBProject). There I have created a stateless remote EJB (name = HelloWorldEJB). I have also created an remote interface there (name= HelloWorldEJBInterfaceRemote). After that I am creating a jar & an ear of the project after compiling everything using ant. Then I deployed the EAR in the WebSphere Application Server 6.1.

Next I have also created a stand alone java project (name = HelloWorldClient), put the jar of the HelloWorldEJBProject to this project build path. Now while I am doing the look up I am getting errors.

HelloWorldEJB.java:

package com.staples.ejb;

import com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote;
import javax.ejb.Stateless;

@Stateless
public class HelloWorldEJB implements HelloWorldEJBInterfaceRemote {


    public HelloWorldEJB() {
    }

    public String helloWorld() {

        return "Hello World";
    }

}

Client.java (Inside HelloWorldClient project):

package com.staples.client.processor;

import com.staples.client.util.ApplicationUtil;
import com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote;

public class Client {

    static private HelloWorldEJBInterfaceRemote helloInterface = ApplicationUtil.getHelloEJBHandle();

    public static void main(String[] args) {
        System.out.println(helloInterface.helloWorld());

    }

}

ApplicationUtil.java (Inside HelloWorldClient project):

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote;

public class ApplicationUtil {

    public static Object getContext(){
        Object obj = null;
        Context context;
        try {
            Hashtable env = new Hashtable();
            env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.websphere.naming.WsnInitialContextFactory");

            env.put(Context.PROVIDER_URL,"corbaloc:iiop:localhost:2811");

            Context ctx = new InitialContext(env);
            obj = ctx.lookup("ejb/HelloEAR/Hello.jar/HelloWorldEJB#" + HelloWorldEJBInterfaceRemote.class.getName());

        } catch (NamingException e) {
            e.printStackTrace();
        }
        return obj;
    }

    public static HelloWorldEJBInterfaceRemote  getHelloEJBHandle()
    {   

        Object obj = getContext();
        HelloWorldEJBInterfaceRemote  helloInterface = (HelloWorldEJBInterfaceRemote) PortableRemoteObject.narrow(obj, HelloWorldEJBInterfaceRemote.class);
        //HelloWorldEJBInterfaceRemote  helloInterface = (HelloWorldEJBInterfaceRemote) obj;
        return  helloInterface;



    }

Error:

Exception in thread "P=141210:O=0:CT" java.lang.ClassCastException: Unable to load class: com.staples.ejb.interfaces._HelloWorldEJBInterfaceRemote_Stub
at com.ibm.rmi.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:372)
at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:156)
at com.staples.client.util.ApplicationUtil.getHelloEJBHandle(ApplicationUtil.java:41)
at com.staples.client.processor.Client.main(Client.java:14)

If I comment the following

HelloWorldEJBInterfaceRemote  helloInterface = (HelloWorldEJBInterfaceRemote) PortableRemoteObject.narrow(obj, HelloWorldEJBInterfaceRemote.class);

& uncomment the following:

HelloWorldEJBInterfaceRemote  helloInterface = (HelloWorldEJBInterfaceRemote) obj;

then the following error comes:

Exception in thread "P=346416:O=0:CT" java.lang.ClassCastException: org.omg.stub.java.rmi._Remote_Stub incompatible with com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote
    at com.staples.client.util.ApplicationUtil.getHelloEJBHandle(ApplicationUtil.java:42)
    at com.staples.client.processor.Client.main(Client.java:14)

I am using EJB 3 so I guess I dont need the PortableRemoteObject.narrow but without using that I get different error. I am not sure also what to write as a argument of context.lookup(). I am pretty new in EJB. Can anybody help me please? Thanks in advance. Thanks to MagicWand & bkail to solve my port & jndi problems.

ᄂ ᄀ
  • 5,669
  • 6
  • 43
  • 57
Sumit
  • 856
  • 5
  • 18
  • 38
  • From which machine are you running your client? Is it on the same machine where WAS resides? If no, then you have to modify ApplicationUtil to point to the right machine to do a JNDI lookup (e.g. corbaloc:iiop:was_machine:2809). Another issue can be with the port: 2809 is default IIOP port, but it is set when creating WAS profile. Please check IIOP port on WAS. – Magic Wand Jun 10 '15 at 13:41
  • Yeah the client resides on the same machine than that of WAS. In my admin console of websphere the BOOTSTRAP_ADDRESS port is 2811. Is this the port you are talking about? Because I cant see any port named IIOP. @MagicWand – Sumit Jun 10 '15 at 13:58
  • After changing it to 2811 the previous error solves. New error is : javax.naming.NameNotFoundException: Context: VDIPN5243Node03Cell/nodes/VDIPN5243Node03/servers/server1, name: ejb/HelloEAR/Hello.jar/HelloWorldEJB/com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote: First component in name HelloWorldEJB/com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0] – Sumit Jun 10 '15 at 13:59
  • Look in SystemOut.log for CNTR0167I messages. If the application started properly, those messages will contain the correct JNDI name for the EJB. – Brett Kail Jun 10 '15 at 14:02
  • The only thing I can found in that log: CNTR0167I: The server is binding the HelloWorldEJBInterfaceRemote interface of the HelloWorldEJB enterprise bean in the Hello.jar module of the HelloEAR application. The binding location is: ejb/HelloEAR/Hello.jar/HelloWorldEJB#com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote @bkail – Sumit Jun 10 '15 at 14:15
  • @SumitPal Yes, that's the lookup string you should use (`HelloWorldEJB#com...` rather than `HelloWorldEJB/com...`). – Brett Kail Jun 10 '15 at 15:51
  • Do you have EJB3 feature pack installed on WAS6.1, since by default, WAS 6.1 doesn't support EJB3? – Gas Jun 11 '15 at 13:29
  • Yeah I have installed that previously @Gas – Sumit Jun 12 '15 at 13:24
  • you can try to run dumpNameSpace.bat (or .sh) to export the whole JNDI name space, then take a look where the EJB is bound, if you don't have any JNDI browser. – Michal Fleischhans Jun 23 '15 at 15:05

1 Answers1

0

After you have started getting NamingException, using com.staples.ejb.interfaces.HelloWorldEJBInt‌​erfaceRemote as your JNDI lookup string should work. Have you tried using that too?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
gshriv
  • 1
  • 1