javax.naming.NoInitialContextException

There are no available Samebug tips for this exception. Do you have an idea how to solve this issue? A short tip would help users who saw this issue last week.

  • The issue produces when I try to reload the configuration from the JMX console. I have a JMS appender defined as follows : <appender name="JMS_TOPIC" class="ch.qos.logback.classic.net.JMSTopicAppender"> <param name="InitialContextFactoryName" value="com.ibm.websphere.naming.WsnInitialContextFactory" /> <param name="ProviderURL" value="iiop://localhost:10014" /> <param name="TopicConnectionFactoryBindingName" value="loggingTopicConnectionFactory" /> <param name="TopicBindingName" value="loggingTopic" /> <param name="LocationInfo" value="false" /> </appender> Exception that I get : 15:15:43,704 |-ERROR in ch.qos.logback.classic.net.JMSTopicAppender[JMS_TOPIC] - Error while activating options for appender named [JMS_TOPIC]. javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory [Root exception is java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory] at javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory at at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:669) at at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:259) at at javax.naming.InitialContext.init(InitialContext.java:235) at at javax.naming.InitialContext.<init>(InitialContext.java:209) at at ch.qos.logback.core.net.JMSAppenderBase.buildJNDIContext(JMSAppenderBase.java:62) at at ch.qos.logback.classic.net.JMSTopicAppender.start(JMSTopicAppender.java:97) at at ch.qos.logback.core.joran.action.AppenderAction.end(AppenderAction.java:96) at at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:315) at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:194) at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:180) at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:52) at at ch.qos.logback.core.joran.spi.Interpreter.play(Interpreter.java:332) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:126) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:93) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:52) at at ch.qos.logback.classic.jmx.JMXConfigurator.reloadByURL(JMXConfigurator.java:149) at at ch.qos.logback.classic.jmx.JMXConfigurator.reloadDefaultConfiguration(JMXConfigurator.java:104) at at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79) at at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at at java.lang.reflect.Method.invoke(Method.java:618) at at com.sun.jmx.mbeanserver.StandardMetaDataImpl.invoke(StandardMetaDataImpl.java:428) at at com.sun.jmx.mbeanserver.MetaDataImpl.invoke(MetaDataImpl.java:238) at at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:833) at at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802) at at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1436) at at javax.management.remote.rmi.RMIConnectionImpl.access$100(RMIConnectionImpl.java:107) at at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1273) at at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1369) at at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:810) at at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source) at at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at at java.lang.reflect.Method.invoke(Method.java:618) at at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:309) at at sun.rmi.transport.Transport$1.run(Transport.java:168) at at java.security.AccessController.doPrivileged(AccessController.java:274) at at sun.rmi.transport.Transport.serviceCall(Transport.java:164) at at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:506) at at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.handleRequest(TCPTransport.java:838) at at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:912) at at java.lang.Thread.run(Thread.java:811) Caused by: java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory at at java.lang.Class.forNameImpl(Native Method) at at java.lang.Class.forName(Class.java:163) at at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:57) at at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:666) at ... 40 common frames omitted The real problem comes from the way the VersionHelper (com.sun.naming.internal.VersionHelper12) loads the InitialContextFactory class. In fact, it uses the Thread.currentThread().getContextClassLoader() method and then passes the result ClassLoader to the Class.forName(String className, boolean initializeBoolean, ClassLoader classLoader) method. Then the ClassNotFoundException is produced and causes the final NoInitialContextException. If it used the default class loader, everything would work fine. The solution that I propose affects the buildJNDIContext() and lookup() methods in the JMSAppenderBase class. The idea is to replace the current thread's ContextClassLoader by the default getClass().getClassLoader() when calling new InitialContext(env) and restore the original one afterwards : public Context buildJNDIContext() throws NamingException { Context jndi = null; final ClassLoader originalLoader = Thread.currentThread().getContextClassLoader(); try { final ClassLoader newLoader = getClass().getClassLoader(); Thread.currentThread().setContextClassLoader(newLoader); // addInfo("Getting initial context."); if (initialContextFactoryName != null) { Properties env = buildEnvProperties(); jndi = new InitialContext(env); } else { jndi = new InitialContext(); } } finally { Thread.currentThread().setContextClassLoader(originalLoader); } return jndi; } protected Object lookup(Context ctx, String name) throws NamingException { final ClassLoader originalLoader = Thread.currentThread().getContextClassLoader(); try { final ClassLoader newLoader = getClass().getClassLoader(); Thread.currentThread().setContextClassLoader(newLoader); return ctx.lookup(name); } catch (NameNotFoundException e) { addError("Could not find name [" + name + "]."); throw e; } finally { Thread.currentThread().setContextClassLoader(originalLoader); } }
    via by Petr Kokorev,
  • The issue produces when I try to reload the configuration from the JMX console. I have a JMS appender defined as follows : <appender name="JMS_TOPIC" class="ch.qos.logback.classic.net.JMSTopicAppender"> <param name="InitialContextFactoryName" value="com.ibm.websphere.naming.WsnInitialContextFactory" /> <param name="ProviderURL" value="iiop://localhost:10014" /> <param name="TopicConnectionFactoryBindingName" value="loggingTopicConnectionFactory" /> <param name="TopicBindingName" value="loggingTopic" /> <param name="LocationInfo" value="false" /> </appender> Exception that I get : 15:15:43,704 |-ERROR in ch.qos.logback.classic.net.JMSTopicAppender[JMS_TOPIC] - Error while activating options for appender named [JMS_TOPIC]. javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory [Root exception is java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory] at javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory at at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:669) at at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:259) at at javax.naming.InitialContext.init(InitialContext.java:235) at at javax.naming.InitialContext.<init>(InitialContext.java:209) at at ch.qos.logback.core.net.JMSAppenderBase.buildJNDIContext(JMSAppenderBase.java:62) at at ch.qos.logback.classic.net.JMSTopicAppender.start(JMSTopicAppender.java:97) at at ch.qos.logback.core.joran.action.AppenderAction.end(AppenderAction.java:96) at at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:315) at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:194) at at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:180) at at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:52) at at ch.qos.logback.core.joran.spi.Interpreter.play(Interpreter.java:332) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:126) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:93) at at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:52) at at ch.qos.logback.classic.jmx.JMXConfigurator.reloadByURL(JMXConfigurator.java:149) at at ch.qos.logback.classic.jmx.JMXConfigurator.reloadDefaultConfiguration(JMXConfigurator.java:104) at at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79) at at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at at java.lang.reflect.Method.invoke(Method.java:618) at at com.sun.jmx.mbeanserver.StandardMetaDataImpl.invoke(StandardMetaDataImpl.java:428) at at com.sun.jmx.mbeanserver.MetaDataImpl.invoke(MetaDataImpl.java:238) at at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:833) at at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802) at at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1436) at at javax.management.remote.rmi.RMIConnectionImpl.access$100(RMIConnectionImpl.java:107) at at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1273) at at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1369) at at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:810) at at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source) at at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at at java.lang.reflect.Method.invoke(Method.java:618) at at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:309) at at sun.rmi.transport.Transport$1.run(Transport.java:168) at at java.security.AccessController.doPrivileged(AccessController.java:274) at at sun.rmi.transport.Transport.serviceCall(Transport.java:164) at at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:506) at at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.handleRequest(TCPTransport.java:838) at at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:912) at at java.lang.Thread.run(Thread.java:811) Caused by: java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory at at java.lang.Class.forNameImpl(Native Method) at at java.lang.Class.forName(Class.java:163) at at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:57) at at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:666) at ... 40 common frames omitted The real problem comes from the way the VersionHelper (com.sun.naming.internal.VersionHelper12) loads the InitialContextFactory class. In fact, it uses the Thread.currentThread().getContextClassLoader() method and then passes the result ClassLoader to the Class.forName(String className, boolean initializeBoolean, ClassLoader classLoader) method. Then the ClassNotFoundException is produced and causes the final NoInitialContextException. If it used the default class loader, everything would work fine. The solution that I propose affects the buildJNDIContext() and lookup() methods in the JMSAppenderBase class. The idea is to replace the current thread's ContextClassLoader by the default getClass().getClassLoader() when calling new InitialContext(env) and restore the original one afterwards : public Context buildJNDIContext() throws NamingException { Context jndi = null; final ClassLoader originalLoader = Thread.currentThread().getContextClassLoader(); try { final ClassLoader newLoader = getClass().getClassLoader(); Thread.currentThread().setContextClassLoader(newLoader); // addInfo("Getting initial context."); if (initialContextFactoryName != null) { Properties env = buildEnvProperties(); jndi = new InitialContext(env); } else { jndi = new InitialContext(); } } finally { Thread.currentThread().setContextClassLoader(originalLoader); } return jndi; } protected Object lookup(Context ctx, String name) throws NamingException { final ClassLoader originalLoader = Thread.currentThread().getContextClassLoader(); try { final ClassLoader newLoader = getClass().getClassLoader(); Thread.currentThread().setContextClassLoader(newLoader); return ctx.lookup(name); } catch (NameNotFoundException e) { addError("Could not find name [" + name + "]."); throw e; } finally { Thread.currentThread().setContextClassLoader(originalLoader); } }
    via by Petr Kokorev,
  • WsnInitialContextFactory in WAS 6.1
    via by xavi1xavi,
    • javax.naming.NoInitialContextException: Cannot instantiate class: com.ibm.websphere.naming.WsnInitialContextFactory at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:669) at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:259) at javax.naming.InitialContext.init(InitialContext.java:235) at javax.naming.InitialContext.<init>(InitialContext.java:209) at ch.qos.logback.core.net.JMSAppenderBase.buildJNDIContext(JMSAppenderBase.java:62) at ch.qos.logback.classic.net.JMSTopicAppender.start(JMSTopicAppender.java:97) at ch.qos.logback.core.joran.action.AppenderAction.end(AppenderAction.java:96) at ch.qos.logback.core.joran.spi.Interpreter.callEndAction(Interpreter.java:315) at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:194) at ch.qos.logback.core.joran.spi.Interpreter.endElement(Interpreter.java:180) at ch.qos.logback.core.joran.spi.EventPlayer.play(EventPlayer.java:52) at ch.qos.logback.core.joran.spi.Interpreter.play(Interpreter.java:332) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:126) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:93) at ch.qos.logback.core.joran.GenericConfigurator.doConfigure(GenericConfigurator.java:52) at ch.qos.logback.classic.jmx.JMXConfigurator.reloadByURL(JMXConfigurator.java:149) at ch.qos.logback.classic.jmx.JMXConfigurator.reloadDefaultConfiguration(JMXConfigurator.java:104) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:79) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:618) at com.sun.jmx.mbeanserver.StandardMetaDataImpl.invoke(StandardMetaDataImpl.java:428) at com.sun.jmx.mbeanserver.MetaDataImpl.invoke(MetaDataImpl.java:238) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:833) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:802) at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1436) at javax.management.remote.rmi.RMIConnectionImpl.access$100(RMIConnectionImpl.java:107) at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1273) at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1369) at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:810) at sun.reflect.GeneratedMethodAccessor71.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:618) at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:309) at sun.rmi.transport.Transport$1.run(Transport.java:168) at java.security.AccessController.doPrivileged(AccessController.java:274) at sun.rmi.transport.Transport.serviceCall(Transport.java:164) at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:506) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.handleRequest(TCPTransport.java:838) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:912) at java.lang.Thread.run(Thread.java:811) Caused by: java.lang.ClassNotFoundException: com.ibm.websphere.naming.WsnInitialContextFactory at java.lang.Class.forNameImpl(Native Method) at java.lang.Class.forName(Class.java:163) at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:57) at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:666) ... 40 more

    Users with the same issue

    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    Unknown visitor1 times, last one,
    17 more bugmates