11 Replies Latest reply on May 21, 2015 9:36 AM by Justin Bertram

    java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl

    Khaleel Ibrahim Newbie

      Hi,

       

      I am facing some issues while upgrading hornetq 2.0.GA to hornetq2.4.0.Final.

       

      When I am trying to create a Topicconnectionfactory object through JNDI connection, I am getting nullpointer exception.While debugging, I got an exception

      " java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl".

       

      I am using Proxy class and using newProxyInstance to create TopicConnectionFactory Object. Please see my code below.

       

       

      ***************************************************************************

      public class ExceptionInterceptorProxyFactory{

         

          //@SuppressWarnings("unchecked")

          public static Object newInstance(Object obj, Class<?>[] interfaces) {

              Object myproxy=Proxy.newProxyInstance(ExceptionInterceptorProxyFactory.class.getClassLoader(),

                      interfaces, new ExceptionInterceptorProxy(obj));

              System.out.println("MyProxy instance is " + myproxy);

              return myproxy;

          }   

         

          private static class ExceptionInterceptorProxy implements InvocationHandler {

             

              private Object obj;

             

              public ExceptionInterceptorProxy(Object obj) {

              //    Proxy.getInvocationHandler(obj);

                  this.obj = obj;

              }

       

              public Object invoke(Object proxy, Method method, Object[] args)

                      throws Throwable{

                

                  try {

                      return method.invoke(obj, args);

                  } catch (InvocationTargetException ivte) {

                      JMSUtil.clearCache();

                      ivte.printStackTrace();

                      throw ivte.getTargetException().getCause();

                  } catch (Throwable e) {

                      JMSUtil.clearCache();

                      throw e;

                  }

              }

       

          }

       

      }

       

      Here the obj what I am getting is HornetQJMSConnectionFactory object. I dont know why I am getting the same. Everywhere in the code I am using TopicConnectionFactory.

      **********************************************************

      This is the Code Flow.

       

      1. NotificationConnectionHandler.java

       

      private static TopicConnection getNewTopicConnection(String url) {

              TopicConnection connection = null;

              try {           

                  connection = JMSUtil.getTopicConnection(url);

                  connection.start();

              } catch (Exception e) {

                  LOG.warn("Cannot create new topic connection", e);

              }

              return connection;

          }

       

      2. JMSUtil.java

       

      private static AbstractJmxUtil utilImpl;

      public static TopicConnection getTopicConnection(String url) throws JMSException

          {

              return utilImpl.getTopicConnection(url);

          }

       

      3. AbstractJmxUtil.java

       

      protected abstract QueueConnectionFactory createQueueConnectionFactory(String url) throws JMSException;

      protected abstract TopicConnectionFactory createTopicConnectionFactory(String url) throws JMSException;

       

      Its Implementation is done in HornetqQueueUtil.java

       

      4. HornetqQueueUtil.java

       

      @Override

          protected TopicConnectionFactory createTopicConnectionFactory(String url)

                  throws JMSException {

              return (TopicConnectionFactory) createConnectionFactory2(url);

          }

      ______________________________________________________________

       

      protected TopicConnectionFactory createConnectionFactory2(String url) throws JMSException

          {

              Properties props = new Properties();

              props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");

              props.put("java.naming.provider.url", url);

              props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

             

              try

              {

                  InitialContext initialCtx = new InitialContext(props);           

                  return (TopicConnectionFactory) initialCtx.lookup("/ConnectionFactory");

              } catch (NamingException e)

              {

                  // Wrap in JMS exception

                  throw new JMSException("Failed to lookup inital context for url - '" + url + "'", e.getMessage());

              }

          }

       

      *********************************

       

      public synchronized TopicConnection getTopicConnection(String url) throws JMSException{   topicconnection= getTopicConnectionFactory(url).createTopicConnection();

      return topicconnection;

      ******************************************************************************************

      Method getTopicConnectionFactory(url) is in AbstractJmxUtil.java

      5. AbstractJmxUtil.java

      protected TopicConnectionFactory getTopicConnectionFactory(String url) throws JMSException{

              synchronized (tcfPool) {

                  if(tcfPool.get(url) == null)

                  {

                     

                      Object value=ExceptionInterceptorProxyFactory.newInstance(createTopicConnectionFactory(url), new Class[]{TopicConnectionFactory.class});

                      //Object value="";

                      //tcfPool.put(url, (TopicConnectionFactory)ExceptionInterceptorProxyFactory.newInstance(createTopicConnectionFactory(url),

                          //    new Class[]{TopicConnectionFactory.class }));

                      tcfPool.put(url, (TopicConnectionFactory) value);

                      System.out.println("Topic Connection pool is " + tcfPool.get(url));

       

                  }

                 

                  return tcfPool.get(url);

              }

             

          }

       

      The method Calls newinstance method of the ExceptionInterceptorProxyFactory class which is written above.

       

       

       

      Please help me resolve the error.

      I am attaching the hornetq configuration files.

        • 1. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
          Khaleel Ibrahim Newbie

          Hi,

           

          Please find attached exception logs. As checked, there is something which I am unable to understand with ClientSessionFactoryImpl and ServerLocatorimpl classes.

           

          Please have a look.

           

          Regards

          Khaleel

          • 2. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
            Justin Bertram Master

            Couple of things:

            1. javax.jms.TopicConnectionFactory is just an interface that has to be implemented by a provider (e.g. HornetQ).  The reason you see org.hornetq.jms.client.HornetQJMSConnectionFactory instances when you use TopicConnectionFactory is because HornetQJMSConnectionFactory is the implementation of that interface.
            2. Make sure you have all the new HornetQ jar files on your classpath.
            3. Regarding the actual problem, please provide something I can easily run to reproduce the issue you're seeing.  It's too difficult to look through your stack-trace and then try to match that with the code you pasted that has no line numbers.  Just provide a reproducible test-case and let the code do the talking.
            • 3. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
              Khaleel Ibrahim Newbie

              Hi,

               

              I am getting an InvocationTargetException while trying to obtain a TopicConnectionFactory Object.

               

              Exception is

               

              java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl.

               

              Under what scenarios can I get these kind of exceptions?

               

              Here I am trying to get the TopicConnectionFactory Object through a proxy instance.

              • 4. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
                Khaleel Ibrahim Newbie

                Earlier my application was using hornetq 2.0.0.ga server where the following jars were used.

                 

                hornetq-core

                hornetq-jms

                jnp-client

                netty

                hornetq-transports

                 

                Also currently I am using the following JAR's client side.

                 

                hornetq-commons-2.3.0.Final

                hornetq-core-client-2.3.0.Final

                hornetq-jms-client-2.3.0.Final

                hornetq-jms-server-2.3.0.Final

                jnp-client-2.3.0.Final

                netty-2.3.0.Final.

                 

                Please let me know whether other JAR's need to be present at run time.

                 

                For JMS API, I am using javaee-api jar.

                 

                Regards,

                Khaleel

                • 5. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
                  Justin Bertram Master

                  The class org.hornetq.core.client.impl.ClientSessionFactoryImpl ships in the hornetq-core-client-<version>.jar file.  If you're getting a NoClassDefFoundError that indicates to me your classpath is not set up properly.

                  • 6. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
                    Khaleel Ibrahim Newbie

                    Hi,

                     

                    The jar is available in my classpath, but still I am getting the same exception. If I try to remove the jar and restart the server, then I am getting ClassNotFoundException which is different from NoClassDefFoundError.

                     

                    Please let me know whether the changes in constructors of ClientSessionFactoryImpl of different hornetq versions can cause this exception.

                     

                    R's

                    Khaleel

                    • 7. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
                      Justin Bertram Master

                      Please let me know whether the changes in constructors of ClientSessionFactoryImpl of different hornetq versions can cause this exception.

                      I can't provide a definitive answer to this question.  Provide me with a reproducible test-case and I can investigate further.

                      • 8. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
                        Khaleel Ibrahim Newbie

                        1. I am trying to create a TopicConnectionFactory

                         

                        protected TopicConnectionFactory createTopicConnectionFactory(String url)

                                    throws JMSException {

                                return createConnectionFactory(url);

                            }

                        *******************************************************************************************************

                        2. ConnectionFactory returned by the above method created through HornetQJMSConnectionFactory

                         

                        protected HornetQJMSConnectionFactory createConnectionFactory(String url) throws JMSException

                            {

                                Properties props = new Properties();

                                props.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");

                                props.put("java.naming.provider.url", url);

                                props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

                               

                                try

                                {

                                    InitialContext initialCtx = new InitialContext(props);           

                                    return (HornetQJMSConnectionFactory)initialCtx.lookup("/ConnectionFactory");

                                } catch (NamingException e)

                                {

                                    // Wrap in JMS exception

                                    throw new JMSException("Failed to lookup inital context for url - '" + url + "'", e.getMessage());

                                }

                            }

                         

                        ******************************************************************************************************************************

                        3. While trying to create TopicConnection, I am using proxy class to cast between HornetQJMSConnectionFactory and TopicConnectionFactory.

                         

                        public synchronized TopicConnection getTopicConnection(String url) throws JMSException{

                        return ((TopicConnectionFactory)getTopicConnectionFactory(url)).createTopicConnection();

                        }

                         

                        //NEXT CODE

                         

                        protected HashMap<String, TopicConnectionFactory> tcfPool = new HashMap<String, TopicConnectionFactory>();

                        protected TopicConnectionFactory getTopicConnectionFactory(String url) throws JMSException{

                                synchronized (tcfPool) {

                                    if(tcfPool.get(url) == null)

                                    {

                                       //THIS IS WHERE I AM USING PROXY INSTANCE

                                        tcfPool.put(url, (TopicConnectionFactory)ExceptionInterceptorProxyFactory.newInstance(createTopicConnectionFactory(url),

                                                new Class[]{TopicConnectionFactory.class }));

                                    }

                                   

                                    return tcfPool.get(url);

                                }

                               

                            }

                         

                        ***********************************************************************************************************************************

                        4. PROXY CLASS DEFINITION IS SHOWN BELOW.


                        import java.lang.reflect.InvocationHandler;

                        import java.lang.reflect.InvocationTargetException;

                        import java.lang.reflect.Method;

                        import java.lang.reflect.Proxy;

                         

                        import com.flytxt.commons.jms.util.JMSUtil;

                         

                        public class ExceptionInterceptorProxyFactory{

                           

                            public static Object newInstance(Object obj, Class[] interfaces) {

                            return Proxy.newProxyInstance(ExceptionInterceptorProxyFactory.class.getClassLoader(),interfaces, new ExceptionInterceptorProxy(obj));

                            }   

                           

                            private static class ExceptionInterceptorProxy implements InvocationHandler {

                               

                                private Object obj;

                               

                                public ExceptionInterceptorProxy(Object obj) {

                                    this.obj = obj;

                                }

                         

                                public Object invoke(Object proxy, Method method, Object[] args)

                                        throws Throwable {

                                    try {

                                        return method.invoke(obj, args);

                                    } catch (InvocationTargetException ivte) {

                                        JMSUtil.clearCache();

                                        throw ivte.getTargetException().getCause();

                                    } catch (Throwable e) {

                                        JMSUtil.clearCache();

                                        throw e;

                                    }

                                }

                         

                            }

                         

                        }

                         

                        Here when I debug the code, I could see that while casting between HornetQJMSConnectionFactory and TopicConnectionFactory, it is throwing InvocationTargetException and the target was

                        java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl


                        **************************************************************************************************************************


                        Please Help.


                        Regards

                        Khaleel

                        • 9. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
                          Justin Bertram Master

                          When I say, "reproducible test-case," I don't mean a bunch of code pasted into a comment.  A reproducible test-case is something I can take and run with relative ease to reproduce the problem you're seeing (and hook up a debugger to it if necessary).  A simple Maven project based on one of the examples we ship in HornetQ would be perfect.

                           

                          Typically if a class can't be initialized that indicates something in its static block threw an exception.  However, org.hornetq.core.client.impl.ClientSessionFactoryImpl has no static block. 

                           

                          I need to be able to reproduce the issue you're seeing to investigate further.  I said this back on May 6, then again on April 9, and now once more.

                          • 10. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
                            Khaleel Ibrahim Newbie

                            As checked, there is no issue with the code as the code perfectly works with hornetq 2.0 jar. It seems like the issue with compatiblilty of hornetq 2.4 or 2.3 with jboss AS 5.1.

                            • 11. Re: java.lang.NoClassDefFoundError: Could not initialize class org.hornetq.core.client.impl.ClientSessionFactoryImpl
                              Justin Bertram Master

                              In any case, I still need to be able to reproduce the problem to investigate further.  Can you provide a test-case?