java – 无法用JBoss服务器实例化InitialContext

我正在尝试创建一个InitialContext,所以我可以向JNDI询问一些企业java bean.
JBoss运行正常,但是当我运行java代码时,我得到了一个异常.

我正在运行JBoss 7.1

这是我的代码:

public class Test {

    public static void main(String[] args){
        InitialContext ctx=getInitialContext();
        Object ref=null;
        try {
            ref = ctx.lookup("ParamEJB/remote");
        } catch (NamingException e) {
            System.out.println("Lookup Failed");
            e.printStackTrace();
        }
        Param stub=(Param)PortableRemoteObject.narrow(ref, Param.class);
        int times=stub.getTimes();
        for(int i=0;i<times;i++)
            System.out.println(stub.getMessage());
    }

    public static InitialContext getInitialContext(){
        Hashtable<String,String> h=new Hashtable<String,String>();
        h.put("java.naming.factory.initial","org.jnp.interfaces.NamingContextFactory");
        h.put("java.naming.provider.url","localhost");
        try {
            return new InitialContext(h);
        } catch (NamingException e) {
            System.out.println("Cannot generate InitialContext");
            e.printStackTrace();
        }
        return null;
    }
    }

在我启动JBoss服务器之后,我尝试运行java代码并得到以下异常:

javax.naming.NoInitialContextException: Cannot instantiate class:     org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException:     org.jnp.interfaces.NamingContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.init(Unknown Source)
at javax.naming.InitialContext.<init>(Unknown Source)
at client.Test.getInitialContext(Test.java:32)
at client.Test.main(Test.java:13)
Caused by: java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)

可能是什么问题?

最佳答案
InitialContext属性不适合您正在使用的JBoss版本.
使用JBoss 7,当您从远程客户端调用ejb时,情况发生了很大变化.
link可以帮助您正确实例化InitialContex对象并确定JNDI条目名称.还将告诉您需要添加到客户端类路径的必要依赖项.

转载注明原文:java – 无法用JBoss服务器实例化InitialContext - 代码日志