import java.rmi.*;
import javax.rmi.*;
import javax.naming.*;
import java.util.*;//Äã×Ô¼ºµÄEJB½Ó¿Ú (TimeFunctions)
import sbtutorial.*;public class EjbClient{

    String url = "t3://192.168.0.67:9001"; 
    String user = null;
    String password = null;
    Properties h = new Properties();    public Context getInitialContext(String url,String user,String password) throws NamingException {
        try{
           h.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
           h.put(Context.PROVIDER_URL,url);
           //h.put(Context.SECURITY_PRINCIPAL,user);
           //h.put(Context.SECURITY_CREDENTIALS,password);
           return new InitialContext(h);
        }catch(NamingException ne)
        {
            throw ne;
        }
    }
    
    public void EjbClient(){             
    }


public static void main(String[] args) {
EjbClient ejbClient = new EjbClient();  
ejbClient.start();                     
}   private void start(){
   
       try{
           Context initial = getInitialContext(url,user,password);
           Object objref = initial.lookup("TimeFunctions");
           TimeFunctionsHome tfHome = (TimeFunctionsHome)PortableRemoteObject.narrow(objref,TimeFunctionsHome.class);
           TimeFunctions tfEjb = tfHome.create();
           System.out.println(tfEjb.getTime());
       }catch(Exception e)
       {
           e.printStackTrace();
           System.out.println(e.getMessage());
       }
   
   
   
   
   
   }
   
   
   
}

解决方案 »

  1.   

    我想知道sbtutorial.*;包里是什么东西啊,
    不过编译器说class TimeFunctionsHome not found,
    当然我不是用你的TimeFunctionsHome而是改成了我自己的接口。
    请问如何解决,谢谢。
      

  2.   

    系统属性中没有放置以下属性:
    INITIAL_CONTEXT_FACTORY和PROVIDER_URL不同的J2EE server要放不同的值,参考你用的服务器手册。
      

  3.   

    是的,就是把ejb的接口class文件copy给client用
      

  4.   

    客户端需要STUB类,不同的容器生成的不同
      

  5.   

    try{
    (Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
            Context ctx = getInitialContext();
         //look up jndi name
            Object ref = ctx.lookup("Enterprise2");
      //cast to Home interface
          Enterprise2Home enterprise2Home = (Enterprise2Home)  PortableRemoteObject.narrow(ref, Enterprise2Home.class);
        Enterprise2 hello=enterprise2Home.create();
        System.out.println(hello.hello());
        jLabel1.setText(hello.hello());
        hello.remove();
        }catch(Exception e){
          e.printStackTrace();
        }
      }
      private Context getInitialContext() throws Exception {
      String url = "t3://localhost:7001";
      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) {
        throw e;
          }
    }
    但运行时老是出现这个异常:
    java.lang.ClassCastException at com.sun.corba.se.internal.javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:296) at javax.rmi.PortableRemoteObject.narrow(PortableRemoteObject.java:137) at ddd1.Frame1.jbInit(Frame1.java:108) at ddd1.Frame1.<init>(Frame1.java:43) at ddd1.Application1.<init>(Application1.java:20) at ddd1.Application1.main(Application1.java:49)
    我不知道哪儿写得不对,该如何改,请指教。
      

  6.   

    把这两句先屏蔽掉看看咯。
    //jLabel1.setText(hello.hello());
      //  hello.remove();