Jakarta ee WebSphere Application Server 6.1的EJB查找问题

Jakarta ee WebSphere Application Server 6.1的EJB查找问题,jakarta-ee,ejb-3.0,jndi,websphere-6.1,ejb-2.x,Jakarta Ee,Ejb 3.0,Jndi,Websphere 6.1,Ejb 2.x,起初我想告诉大家,这里也有人问过类似的问题,但我仍然找不到解决问题的办法 我有一个ejb3项目(name=HelloWorldEJBProject)。在那里,我创建了一个无状态远程EJB(name=HelloWorldEJB)。我还在那里创建了一个远程接口(name=HelloWorldEJBInterfaceRemote)。在那之后,我创建了一个jar&在使用ant编译完所有内容之后,它是一个项目的ear。然后,我在WebSphereApplicationServer6.1中部署了EAR 接下

起初我想告诉大家,这里也有人问过类似的问题,但我仍然找不到解决问题的办法

我有一个ejb3项目(name=HelloWorldEJBProject)。在那里,我创建了一个无状态远程EJB(name=HelloWorldEJB)。我还在那里创建了一个远程接口(name=HelloWorldEJBInterfaceRemote)。在那之后,我创建了一个jar&在使用ant编译完所有内容之后,它是一个项目的ear。然后,我在WebSphereApplicationServer6.1中部署了EAR

接下来,我还创建了一个独立的java项目(name=HelloWorldClient),将HelloWorldEJBProject的jar放在这个项目构建路径中。现在,当我进行查找时,我发现了错误

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";
    }

}
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());

    }

}
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;



    }
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)
Client.java(HelloWorldClient项目内部):

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";
    }

}
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());

    }

}
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;



    }
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)
ApplicationUtil.java(HelloWorldClient项目内部):

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";
    }

}
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());

    }

}
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;



    }
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)
错误:

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";
    }

}
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());

    }

}
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;



    }
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)
如果我评论一下

HelloWorldEJBInterfaceRemote  helloInterface = (HelloWorldEJBInterfaceRemote) PortableRemoteObject.narrow(obj, HelloWorldEJBInterfaceRemote.class);
&取消对以下内容的注释:

HelloWorldEJBInterfaceRemote  helloInterface = (HelloWorldEJBInterfaceRemote) obj;
然后出现以下错误:

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)

我正在使用EJB3,所以我想我不需要PortableRemoteObject.窄带,但是如果不使用它,我会得到不同的错误。我也不确定作为context.lookup()的参数要写什么。我对EJB很陌生。有人能帮我吗?提前谢谢。感谢MagicWand&bkail解决了我的端口和jndi问题。

在您开始使用
com.staples.ejb.interfaces.HelloWorldEJBInt获取
NamingException
后‌​erfaceRemote
作为
JNDI
查找字符串应该可以工作。您也尝试过使用它吗?

您在哪台机器上运行客户端?它是否位于WAS所在的同一台机器上?如果没有,那么您必须修改application直到指向正确的机器以执行JNDI查找(例如corbaloc:iiop:was_machine:2809)。端口可能存在另一个问题:2809是默认的IIOP端口,但它是在创建WAS配置文件时设置的。请检查WAS上的IIOP端口。是的,客户端与WAS驻留在同一台计算机上。在我的websphere管理控制台中,BOOTSTRAP_地址端口是2811。这就是你说的港口吗?因为我看不到任何名为IIOP的端口@magicwand将其更改为2811后,上一个错误解决。新的错误是:javax.naming.NameNotFoundException:Context:VDIPN5243Node03Cell/nodes/VDIPN5243Node03/servers/server1,名称:ejb/HelloEAR/Hello.jar/HelloWorldEJB/com.staples.ejb.interfaces.HelloWorldEJB/com.staples.ejb.interfaceremote未找到名为HelloWorldEJB的第一个组件。[根异常为org.omg.CosNaming.NamingContextPackage.NotFound:IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]在SystemOut.log中查找CNTR0167I消息。如果应用程序正确启动,这些消息将包含EJB的正确JNDI名称。我在该日志中找到的唯一内容是:CNTR0167I:服务器正在绑定HelloEAR应用程序的Hello.jar模块中HelloWorldEJB企业bean的HelloWorldEJBInterfaceRemote接口。绑定位置是:ejb/HelloEAR/Hello.jar/HelloWorldEJB#com.staples.ejb.interfaces.HelloWorldEJBInterfaceRemote@bkail