//Remote接口
public interface HelloWord extends EJBObject {
public String hello() throws RemoteException;
}
//Home接口
public interface HelloWordHome extends EJBHome {
public HelloWord creat() throws RemoteException,CreateException;
}
//SessionBean
public class HelloWordBean implements SessionBean {
private SessionContext context;
private String words; public HelloWordBean() {} public void ejbActivate() throws EJBException, RemoteException {} public void ejbPassivate() throws EJBException, RemoteException {} public void ejbRemove() throws EJBException, RemoteException {} public void setSessionContext(SessionContext newContext)
throws EJBException {
context = newContext;
}
public void ejbCreate() throws CreateException{
words = "成功了";
}

public String hello(){
System.out.println("第一个EJB例子" + words);
return words;
}
public void replaceWithRealBusinessMethod() throws EJBException {}}
然后部署到glassfish中,究竟是打成jar包还是war包
接着又写了个测试程度调用
public class HelloClient { public static void main(String[] args) throws NamingException, RemoteException, CreateException {
try{
//初始化Context实例
 InitialContext ic = new InitialContext();
//执行JNDI查找
 HelloWordHome home =(HelloWordHome)ic.lookup("com.Kingdosoft.zjj.HelloWordHome");
System.out.println("完成JNDI查找");
//调用Home接口的creat方法,返回Remote接口实例
HelloWord hw = home.creat();
System.out.println("完成HelloWord的Remote接口的创建");
//调用业务逻辑方法
System.out.println(hw.hello());
}catch(Exception e){
e.printStackTrace();
}
}
}
再然后就抛异常了
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
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at com.Kingdosoft.zjj.HelloClient.main(HelloClient.java:27)
就是执行jndi查找有问题
有高手能解决下么。。小弟感激不尽

解决方案 »

  1.   

    客户端调用EJB 时,,,初始化上下文需要配置几个东西: Context ctx = null;
            try {
                String url = "t3://localhost:7001";
                String user = "weblogic";//weblogic 管理用户名
                String password = "weblogic";//weblogic 管理密码
                Properties props = new Properties();
                props.put(Context.PROVIDER_URL,url);
                props.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
                props.put(Context.SECURITY_PRINCIPAL,user);
                props.put(Context.SECURITY_CREDENTIALS,password);
                ctx = new InitialContext(props);
                ctx.lookup(.......);              .......
            } catch (Exception ex) {
                ex.printStackTrace();
            }