奇怪,刚刚我不是回答过一次吗?怎么没有了!!!!
视乎你使用的不同的应用服务器,你必须设置
java.naming.factory.initialtialContextFactory
java.naming.provider.url
java.naming.security.principal
java.naming.security.credentials
这几个属性,至于对应的值是什么就各有不同了,可以参考应用服务器的文档
weblogic:
java.naming.factory.initial=weblogic.jndi.WLInitialContextFactory
java.naming.provider.url=t3://localhost:7001
java.naming.security.principal=system
java.naming.security.credentials=password
最好就是使用一个JNDI.Properties文件来进行配置
那么上面那段程序就什么也不用改,只要增加一个jndi.properties文件

解决方案 »

  1.   

    对于这段代码:
    MyHome Home=(MyHome)ctx.lookup("MyHome");
    最好通过javax.rmi.PortableRemoteObject.PortableRemoteObject去narrow得到home对象.
      

  2.   

    ....
    Properties props=System.getProperties();
    Context ctx=new InitialContext(props);
    Object ref = ctx.lookup("MyHome");
    MyHome home = (HelloHome)javax.rmi.PortableRemoteObject.PortableRemoteObject.narrow(ref,MyHome.class);
    ....
    你看看这么写应该没问题的。
      

  3.   

    首先需要设置javax.naming.*
    其次需要narrow一下。home不能简单的用单括号强定义!楼上说的很对。
      

  4.   

    我试了,但是还不行,显示
    javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial到底是什么问题,而且在编译的时候出现两个警告
    1  "EjbStateless.ejbgrpx": Spaces in the temporary directory path may cause WebLogic EJBC utility to produce fatal compile errors.
    2  "EjbStateless.ejbgrpx": Spaces in the classpath may cause WebLogic EJBC utility to produce fatal compile errors.
    可不可能是weblogic配置有问题,为这个问题忙了两天了,大家救命了!!
      

  5.   

    import javax.naming.*;
    import java.util.Properties;
    import javax.rmi.PortableRemoteObject;public class HelloClient extends Object {
      private static final String ERROR_NULL_REMOTE = "Remote interface reference is null.  It must be created by calling one of the Home interface methods first.";
      private static final int MAX_OUTPUT_LINE_LENGTH = 100;
      private boolean logging = true;
      private HelloHome helloHome = null;
      private Hello hello = null;  //Construct the EJB test client
      public HelloClient() {
        initialize();
      }  public void initialize() {
        long startTime = 0;
        if (logging) {
          log("Initializing bean access.");
          startTime = System.currentTimeMillis();
        }    try {
          //get naming context
          Context context = getInitialContext();      //look up jndi name
          Object ref = context.lookup("hello");
          //look up jndi name and cast to Home interface
          helloHome = (MyHome) PortableRemoteObject.narrow(ref, MyHome.class);
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded initializing local bean access through Local Home interface.");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed initializing bean access.");
          }
          e.printStackTrace();
        }
      }  private Context getInitialContext() throws Exception {
        String url = "t3://fileserver:7001";      //Web server address and port number
        String user = null;
        String password = null;
        Properties properties = null;
        try {
          properties = new Properties();
          properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
          properties.put(Context.PROVIDER_URL, url);
          if (user != null) {
            properties.put(Context.SECURITY_PRINCIPAL, user);
            properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
          }      return new InitialContext(properties);
        }
        catch(Exception e) {
          log("Unable to connect to WebLogic server at " + url);
          log("Please make sure that the server is running.");
          throw e;
        }
      }  //----------------------------------------------------------------------------
      // Methods that use Home interface methods to generate a Remote interface reference
      //----------------------------------------------------------------------------  public Hello create() {
        long startTime = 0;
        if (logging) {
          log("Calling create()");
          startTime = System.currentTimeMillis();
        }
        try {
          hello = helloHome.create();
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: create()");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: create()");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from create(): " + hello + ".");
        }
        return hello;
      }  //----------------------------------------------------------------------------
      // Methods that use Remote interface methods to access data through the bean
      //----------------------------------------------------------------------------  public String hello() {
        String returnValue = "";    if (hello == null) {
          System.out.println("Error in hello(): " + ERROR_NULL_REMOTE);
          return returnValue;
        }    long startTime = 0;
        if (logging) {
          log("Calling hello()");
          startTime = System.currentTimeMillis();
        }    try {
          returnValue = hello.hello();
          if (logging) {
            long endTime = System.currentTimeMillis();
            log("Succeeded: hello()");
            log("Execution time: " + (endTime - startTime) + " ms.");
          }
        }
        catch(Exception e) {
          if (logging) {
            log("Failed: hello()");
          }
          e.printStackTrace();
        }    if (logging) {
          log("Return value from hello(): " + returnValue + ".");
        }
        return returnValue;
      }  //----------------------------------------------------------------------------
      // Utility Methods
      //----------------------------------------------------------------------------  private void log(String message) {
        if (message == null) {
          System.out.println("-- null");
          return ;
        }
        if (message.length() > MAX_OUTPUT_LINE_LENGTH) {
          System.out.println("-- " + message.substring(0, MAX_OUTPUT_LINE_LENGTH) + " ...");
        }
        else {
          System.out.println("-- " + message);
        }
      }
      //Main method  public static void main(String[] args) throws Exception{
        HelloClient client = new HelloClient();
        //Properties props=System.getProperties();
        //Context ctx=new InitialContext(props);
        //Object obj=ctx.lookup("HelloHome");
        //HelloHome Home=(HelloHome)javax.rmi.PortableRemoteObject.narrow(obj, HelloHome.class);
        //Hello hello=Home.create();
        //System.out.println(hello.hello());
        //hello.remove();
        client.create();
        String retValue=client.hello();
        System.out.println(retValue);
      }
    }
      

  6.   

    与服务器的通信解决了
    但在查找时发生了Error:
    javax.naming.NameNotFoundException: Unable to resolve 'HelloHome' Resolved: '' Unresolved:'HelloHome' ; remaining name 'HelloHome'
    能告诉我为什么吗?
    而且我是在开启Weblogic时运行的,我的JBuilder8+weblogic 7配置有问题吗?大家帮忙就帮到底了,小弟先谢了!
      

  7.   

    老弟,不是'HelloHome'吧,应该是你的EJB类名啊,你怎么能去lookup一个home接口呢,如果你的EJB名字叫HelloBean,那么你应该lookup("HelloBean")才对啊!
      

  8.   

    Enterprise JavaBean 客户机可以是独立的应用程序、Servlet、Applet 或甚至是其它 Enterprise Bean。例如,以前显示的 HotelClerk 会话 Bean 是 Room 实体 Bean 的客户机。所有客户机都使用服务器 bean 的本地接口来获取对服务器 bean 的引用。这个引用拥有服务器 bean 的远程接口数据类型,因此客户机通过远程接口中定义的方法单独与服务器 bean 交互。 远程接口定义了诸如用于更改客户名称的读方法和写方法的商业方法,或用于执行任务的商业方法,如使用 HotelClerk bean 在旅馆预定房间。以下是如何从客户机应用程序访问 Customer bean 的示例。在本例中,本地接口是 CustomerHome,远程接口是 Customer。 
    CustomerHome home;
    Object       ref;// Obtain a reference to the CustomerHome
    ref  = jndiContext.lookup("java:comp/env/ejb/Customer");// Cast object returned by the JNDI lookup to the
    // appropriate datatype
    home = PortableRemoteObject.narrow(ref, CustomerHome.class);// Use the home interface to create a
    // new instance of the Customer bean.
    Customer customer = home.create(customerID);// Use a business method on the Customer.
    customer.setName(someName);客户机首先使用 JNDI ENC 查找服务器 bean,以获取对本地接口的引用。在 EJB 1.1 中,Java RMI-IIOP 是指定编程模块。因而,必须支持所有 CORBA 引用类型。不能使用 Java 本地强制转型来强制转型 CORBA 引用。事实上,PortableRemoteObject.narrow() 方法必须用于明确将引用的范围从一种类型缩小到其子类型。由于 JNDI 始终返回 Object 类型,因此应该明确缩小所有 bean 引用的范围以便支持容器间的可移植性。 获取了本地接口之后,客户机使用本地接口上定义的方法来创建、查找或除去服务器 bean。调用本地接口上的一个 create() 方法将向客户机返回一个服务器 bean 的远程引用,客户机将使用该远程引用来完成任务。这是ibm一个教程里的内容