initialContext.lookup() - javax.naming.NoInitialContextException

时间:2014-06-06 05:58:55

标签: java ejb

我正在尝试从我的客户端代码访问ejb方法sayHello() 我做了什么: 1:创建了一个HelloWorld接口

    package remoteif;
    import java.rmi.*;
    import javax.ejb.*;
    public interface HelloWorld extends EJBObject{
    public String sayHello() throws RemoteException;
     }

2:HelloWorldHome

package homeif;
import remoteif.HelloWorld;
import javax.ejb.CreateException;
import javax.ejb.EJBHome;
import java.rmi.RemoteException;
public interface HelloWorldHome extends EJBHome {
    public HelloWorld create() throws
            CreateException, RemoteException;
}

3:Bean实现HelloWorldBean

package beanimpl;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import java.rmi.RemoteException;
public class HelloWorldBean implements SessionBean {
    // Methods of Remote interface
    public String sayHello() {
        return "Hello, world !";
    }
    // Methods of Home interface
    public void ejbCreate() {}
    protected SessionContext ctx;
    public void setSessionContext(SessionContext ctx) {
        this.ctx = ctx;
    }
    @Override
    public void ejbRemove() throws EJBException, RemoteException {
        //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
    public void ejbActivate() throws EJBException, RemoteException {
        //To change body of implemented methods use File | Settings | File Templates.
    }
    @Override
    public void ejbPassivate() throws EJBException, RemoteException {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

4:部署描述符:jonas-ejb-jar.xml& ejb-jar.xml
5:通过包含这些类,接口和xml文件来创建jar文件 6:创建了一个客户端应用程序&将创建的jar添加为库文件
7.创建HelloClient

package client;
import homeif.HelloWorldHome;
import remoteif.HelloWorld;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
public class HelloClient {
    public static void main(String args[]) {
        try {
            Context initialContext = new InitialContext();
            Object objref = initialContext.lookup("myHelloWorld");
            System.out.println("3");
            HelloWorldHome home =
                    (HelloWorldHome) PortableRemoteObject.narrow(objref,
                            HelloWorldHome.class);
            HelloWorld myHelloWorld = home.create();
            String message = myHelloWorld.sayHello();
            System.out.println(message);
        } catch (Exception e) {
            System.err.println(" Error : " + e);
            System.exit(2);
        }
    }
}

但在运行时,我正在

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
    
    我怎么解决这个问题?

2 个答案:

答案 0 :(得分:0)

在类路径中创建一个名为jndi.properties的文件,并添加以下行:

java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory
java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces
java.naming.provider.url=jnp://DEIN_EXTERNER_SERVER:1099

这将解决您的问题。

答案 1 :(得分:0)

您已将此主题标记为已回答,但我想说,在我看来,您必须将您的bean注释为@LocalBean或将您的接口注释为@Remote / @Local以拥有远程/本地bean。