问题描述
错误信息如下:18:04:02,358 ERROR [STDERR] javax.naming.NoInitialContextException: Cannot instantiate class: org.jnp.interfaces.NamingContextFactory [Root exception is java.lang.ClassNotFoundException: org.jnp.interfaces.NamingContextFactory ]18:04:02,359 ERROR [STDERR] at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)18:04:02,359 ERROR [STDERR] at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)18:04:02,359 ERROR [STDERR] at javax.naming.InitialContext.init(Unknown Source)18:04:02,359 ERROR [STDERR] at javax.naming.InitialContext.<init>(Unknown Source)18:04:02,359 ERROR [STDERR] at servlet.SampleServlet.initBean(SampleServlet.java:92)出现错误的代码如下:package servlet;......public class SampleServlet extends HttpServlet{......private void initBean()throws ServletException{try{Properties properties = new Properties();//这里报错properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory ");properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces ");properties.setProperty(Context.PROVIDER_URL, "localhost:1099"); initialContext = new InitialContext(properties);Object objref = initialContext.lookup("security-sample/UserManagement");Object obj = PortableRemoteObject.narrow(objref,UserManagementHome.class);home = (UserManagementHome)obj;bean = home.create();}catch (CreateException e1) {e1.printStackTrace();throw new ServletException(e1.getMessage());}catch(NamingException ne){ne.printStackTrace();throw new ServletException(ne.getMessage());}catch (RemoteException e) { e.printStackTrace();}}......}在网上找了各种方法,都试过了,都不好使。1、把javaall-client.jar、jnp-client.jar文件放到Java Build Path里2、classpath里加%JBoss Home%clientjbossall-client.jar都不好使,希望高手能给予帮助 问题补充:zhangjiai1 写道
解决方案
第一,不需要拷贝任何包。因为你的war部署在jboss中,jboss已经加载了client中的jar第二,出现错误的原因是引用properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory "); properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces "); 多了一个空格
解决方案二:
好像也不是空格的原因哦。