//本类需要引入的类或接口
import java.rmi.RemoteException;
import java.util.Properties;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;/**
 * 这是EJB的客户端测试程序
 */public class HelloClient {  private static final String JNDI_NAME = "HelloHome";  private String url;
  private HelloHome home;  public HelloClient(String url)
    throws NamingException
  {    this.url       = url;
    
    home = lookupHome();
  }
  
  void test()
    throws RemoteException,CreateException
  {
    Hello hello = (Hello) PortableRemoteObject.narrow(home.create(), Hello.class);
    System.out.println("I am in client. "+hello.sayHello());
  }
  /**
   * 运行这个实例:
   * java HelloClient t3://localhost:7001
   */
  public static void main(String[] args) throws Exception {    System.out.println("\n客户端程序测试开始...\n");    String url       = "t3://localhost:7001";
    
    // 解析命令行
     if (args.length != 1) {
      System.out.println("用法: java HelloClient t3://hostname:port");   
      return;
    } else {
      url = args[0];
    }
    HelloClient client = null;
    try {
      client = new HelloClient(url);
    } catch (NamingException ne) {
      System.exit(1);
    }
    try {
      client.test();
    } catch (Exception e) {
     System.exit(1);
    } 
    System.out.println("\n客户端程序测试结束...\n");
  }
  /**
   * 查找 EJB 的主接口
   */
  private HelloHome lookupHome()
    throws NamingException
  {
     Context ctx = getInitialContext();
     Object home = ctx.lookup(JNDI_NAME);
     return (HelloHome) PortableRemoteObject.narrow(home, HelloHome.class);
  }  /**
   * 使用属性对象获取上下文
   */
  private Context getInitialContext() throws NamingException {
      Properties h = new Properties();
      h.put(Context.INITIAL_CONTEXT_FACTORY,
         "weblogic.jndi.WLInitialContextFactory");
      h.put(Context.PROVIDER_URL, url);
      return new InitialContext(h);
  }
}

解决方案 »

  1.   

    说明你的try {
          client.test();
        } catch (Exception e) {
         System.exit(1);
        } 
        部分发生了异常,你在看看server中的例子,多调试一下。我想应没什么大的问题。
      

  2.   

    try {
          client = new HelloClient(url);
        } catch (NamingException ne) {
    必须由home接口生成ejb对象的引用,不能自己new。client  = home.create();你先用jbuilder自己生成的testclient程序,运行一下,确信配置没错。