-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 29 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: java.lang.NoClassDefFoundError only in Eclipse - Solved!
PostPosted: Tue Jul 25, 2006 11:44 pm 
Newbie

Joined: Tue Jul 25, 2006 6:39 pm
Posts: 16
Location: Dallas, TX
Environment
Eclipse Version: 3.2.0
WTP 1.5
Linux 2.6.10-5-k7
ubuntu breezy
Hibernate 3.1.3
java.vm.vendor=Sun Microsystems Inc.
java.vm.version=1.5.0_04-b05

Ok, here is the problem which I only get this issue inside of Eclipse. From the command line I get way past this issue.

In the code below I only get to new Configuration() when things go wrong. See code and excpetion below.

Code:
        try
        {
            // Create the SessionFactory from hibernate.cfg.xml
            Configuration c= new Configuration();
            c.configure();
            sessionFactory = c.buildSessionFactory();
           
            logInfo("AnalyzeRawTripData Initialized");
        }
          
        catch (Throwable ex)
        {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            logException("unable to  Initialize AnalyzeRawTripData",ex);
        }


exception
Code:
java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
   at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:116)
   at com.travelbahn.xgwStats.AnalyzeRawTripData.init(AnalyzeRawTripData.java:38)
   at com.travelbahn.xgwStats.XGWStatsProcess.process(XGWStatsProcess.java:53)
   at com.travelbahn.xgwStats.XGWStatsProcess.main(XGWStatsProcess.java:196)


The exception would make you think that I don't have the hibernate-3.1/lib/commons-logging-1.0.4.jar setup in the build path for this project, but I do! Can't seem to get very far with this and I've even traced this down into the hibernate lib.

Code:
public class Configuration implements Serializable {

   private static Log log = LogFactory.getLog( Configuration.class );


Seem to bomb out in the LogFactory code. Funny thing is you get way down into the getLog() method when it trys to load the default wraper for log4j and the class loader fails by returning null.

Again, if I run this app from the command line using a simple script everything goes along just fine.

Another thing I've tried is
Code:
    try {
   Class.forName("org.apache.commons.logging.LogFactory");
   }


as you'd expect this bombs!


Last edited by gxwilson on Thu Jul 27, 2006 7:39 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 3:15 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
this is an eclipse/osgi classloader issue.

look for buddy classloading and other fun stuff to get an detailed explanation.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 2:52 pm 
Newbie

Joined: Tue Jul 25, 2006 6:39 pm
Posts: 16
Location: Dallas, TX
Max, I tried the suggestion you gave and ultimately got to http://hibernate.org/311.html and follwed the instructions of modifying the org.hibernate.eclipse_3.1.0.alpha5.jar manifest file as described and adding a manifest file to my project with the appropriate line described. No joy!

I'm not that experienced with Eclipse so some of jargen is confusing. In my case I have simple java project not a plugin or rich client.

I also played with my build path in Eclipse project and got to the point of not seeing the same error for the LogFactory but did get the following error in the constructor still of Configuration class same as my previous post.

The exception
Code:
java.lang.ExceptionInInitializerError
   at com.travelbahn.xgwStats.AnalyzeRawTripData.init(AnalyzeRawTripData.java:50)
   at com.travelbahn.xgwStats.XGWStatsProcess.process(XGWStatsProcess.java:53)
   at com.travelbahn.xgwStats.XGWStatsProcess.main(XGWStatsProcess.java:196)
Caused by: org.apache.commons.logging.LogConfigurationException: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException) (Caused by org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException))
   at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:543)
   at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:235)
   at org.apache.commons.logging.impl.LogFactoryImpl.getInstance(LogFactoryImpl.java:209)
   at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:351)
   at org.hibernate.cfg.Configuration.<clinit>(Configuration.java:116)
   ... 3 more
Caused by: org.apache.commons.logging.LogConfigurationException: java.lang.NullPointerException (Caused by java.lang.NullPointerException)
   at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:397)
   at org.apache.commons.logging.impl.LogFactoryImpl.newInstance(LogFactoryImpl.java:529)
   ... 7 more
Caused by: java.lang.NullPointerException
   at org.apache.commons.logging.impl.LogFactoryImpl.getLogConstructor(LogFactoryImpl.java:374)
   ... 8 more


Again, I've traced this down into the bowls of common-logging api into org.apache.commons.logging.impl.LogFactory in the method getLogConstructor(). What happens in the line logInterface = this.getClass().getClassLoader().loadClass the first call this.getClass() returns null and thus as the stack unwraps you get the exception above.

Code:
    protected Constructor getLogConstructor()
        throws LogConfigurationException {

        // Return the previously identified Constructor (if any)
        if (logConstructor != null) {
            return logConstructor;
        }

        String logClassName = getLogClassName();

        // Attempt to load the Log implementation class
        Class logClass = null;
        Class logInterface = null;
        try {
            logInterface = this.getClass().getClassLoader().loadClass  [b]<-- this.getClass returns null[/b]
                (LOG_INTERFACE);
            logClass = loadClass(logClassName);
            if (logClass == null) {
                throw new LogConfigurationException
                    ("No suitable Log implementation for " + logClassName);
            }
            if (!logInterface.isAssignableFrom(logClass)) {
                Class interfaces[] = logClass.getInterfaces();
                for (int i = 0; i < interfaces.length; i++) {
                    if (LOG_INTERFACE.equals(interfaces[i].getName())) {
                        throw new LogConfigurationException
                            ("Invalid class loader hierarchy.  " +
                             "You have more than one version of '" +
                             LOG_INTERFACE + "' visible, which is " +
                             "not allowed.");
                    }
                }
                throw new LogConfigurationException
                    ("Class " + logClassName + " does not implement '" +
                     LOG_INTERFACE + "'.");
            }
        } catch (Throwable t) {
            throw new LogConfigurationException(t);
        }

        // Identify the <code>setLogFactory</code> method (if there is one)
        try {
            logMethod = logClass.getMethod("setLogFactory",
                                           logMethodSignature);
        } catch (Throwable t) {
            logMethod = null;
        }

        // Identify the corresponding constructor to be used
        try {
            logConstructor = logClass.getConstructor(logConstructorSignature);
            return (logConstructor);
        } catch (Throwable t) {
            throw new LogConfigurationException
                ("No suitable Log constructor " +
                 logConstructorSignature+ " for " + logClassName, t);
        }
    }


Still looking for help if anyone has any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 3:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
ok - is it a plugin or an ordinary java project we are talking about ?

buddy classloading does not make sense for ordinary java projects.

The issue with a NPE commons logging i don't know anything about.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 4:05 pm 
Newbie

Joined: Tue Jul 25, 2006 6:39 pm
Posts: 16
Location: Dallas, TX
This is an ordinary java project

Let me also say this only happens in Eclipse. Using a small script to run the app from the command line seems to work just fine

I've built other apps in Eclipse which do straight JDBC and everything is good there. My only issue is with running Eclipse and Hibernate together. Odd thing is I can use the Hibernate tools (for the most part) with little trouble but can't run the code.

One other piece of info is if I put the following just before the contructor; works but it still fails in the hibernate constructor.

Code:
try {
   Class.forName("org.apache.commons.logging.LogFactory");
   }


Last edited by gxwilson on Wed Jul 26, 2006 7:24 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 26, 2006 4:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
so i would validate that you don't have duplicate/non-compatible jars in your claspath

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 9:19 am 
Newbie

Joined: Tue Jul 25, 2006 6:39 pm
Posts: 16
Location: Dallas, TX
Max, other than the multiple instances of commons-logging*.jar's in the plugins within eclipse everything seems clean.

I put a few more notes above in my previous posting as well.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 9:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh - what more notes ? I can't remember the diff ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 10:34 am 
Newbie

Joined: Tue Jul 25, 2006 6:39 pm
Posts: 16
Location: Dallas, TX
Sorry, I wasn't thinking.

The new stuff is below --V

Let me also say this only happens in Eclipse. Using a small script to run the app from the command line seems to work just fine

I've built other apps in Eclipse which do straight JDBC and everything is good there. My only issue is with running Eclipse and Hibernate together. Odd thing is I can use the Hibernate tools (for the most part) with little trouble but can't run the code.

One other piece of info is if I put the following just before the contructor; works but it still fails in the hibernate constructor.
Code:
try {
   Class.forName("org.apache.commons.logging.LogFactory");
   }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 1:42 pm 
Newbie

Joined: Tue Jul 25, 2006 6:39 pm
Posts: 16
Location: Dallas, TX
So, I've stripped this thing down to just the following code which points me to an issue with commons-logging. What I've found is that commons-logging (v1.0.4) is used and several of the Eclipse plugins and even inside of the org.hibernate.eclipse_3.1.0.alpha5.jar

Code:
Log log = LogFactory.getLog( Configuration.class );


So with no other Hibernate code in my application other than the above. I still get the exact same exception.

Code:
Log log = LogFactory.getLog( "bob" );


Same excpetion

If I remove all other plugins that include commons-logging*.jar from my Eclipse/plugin directory and still get the exception.

Remove the Hibernate Tools plugin... still get the same exception!

I'm hoping others are learning from this but I'm still at a loss to fix it.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 2:16 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eclipse doesn't do weird things unless you ask it to ;)

you mentioned conflicting commons logging versions - that will definitly be an explanation for some of these errors you see.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 27, 2006 7:38 pm 
Newbie

Joined: Tue Jul 25, 2006 6:39 pm
Posts: 16
Location: Dallas, TX
Mystery SOLVED!

I setup my initial user library for Hibernate with all of the jar files included in a single user library entry.

If you setup individual user libraries for each needed jar for your class path it works!

Unfortunately, a week of my life I'll never get back...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 1:53 pm 
Newbie

Joined: Tue Sep 05, 2006 1:49 pm
Posts: 1
wilson,

I am exactly getting the same error.I have an ear file with commons-loggin.jar and log4j(as our application uses log4j).As hibernate uses commons jar,i have included in the ear.

I dint quite understand your solution,please help.

krishna


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 08, 2006 9:53 am 
Newbie

Joined: Tue Jul 25, 2006 6:39 pm
Posts: 16
Location: Dallas, TX
In your Eclipse project how are your external libraries included in your project?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 18, 2006 4:28 pm 
Newbie

Joined: Mon Sep 18, 2006 4:25 pm
Posts: 1
thank you gxwilson!


>>Unfortunately, a week of my life I'll never get back...
for me it was 4 hours....


best regards,
jarek


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 29 posts ]  Go to page 1, 2  Next

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.