一、异常描述
通过客户端client测试的远程Remote的EJB服务(部署在weblogic容器),报出javax.naming.NoInitialContextException:Cannot instantiate class: weblogic.jndi.WLInitialContextFactory,具体异常日志如下所示
Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]@b@ at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)@b@ at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)@b@ at javax.naming.InitialContext.init(InitialContext.java:223)@b@ at javax.naming.InitialContext.<init>(InitialContext.java:197)@b@ at com.xwood.ejb.client.HelloAbcClient.main(HelloAbcClient.java:18)@b@Caused by: java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory@b@ at java.net.URLClassLoader$1.run(URLClassLoader.java:202)@b@ at java.security.AccessController.doPrivileged(Native Method)@b@ at java.net.URLClassLoader.findClass(URLClassLoader.java:190)@b@ at java.lang.ClassLoader.loadClass(ClassLoader.java:306)@b@ at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)@b@ at java.lang.ClassLoader.loadClass(ClassLoader.java:247)@b@ at java.lang.Class.forName0(Native Method)@b@ at java.lang.Class.forName(Class.java:249)@b@ at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)@b@ at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)@b@ ... 4 more
二、解决步骤
1. 根据ClassNotFoundException异常,判定缺少weblogic.jndi.WLInitialContextFactory依赖包造成的,如下图,加载weblogic相对目录..\Oracle\Middleware\wlserver_10.3\server\lib\wlclient.jar(下载)依赖包,另外服务端依赖包javax.ejb_3.0.1.jar(下载)
2. 如上图在项目引入缺失包wlclient.jar包,运行结果正常,如下图所示