8 Replies Latest reply: Oct 23, 2006 2:09 PM by 807607 RSS

    java.lang.NoClassDefFoundError only occurs sometimes

    807607
      Hi, All,

      I'm hoping some of you Java experts can help me out. My program cycles through a listing of data. Each of the object is supposed to have identical format. The code works great for most of the objects. However, when I try to process a couple of them, I get the following:

      java.lang.NoClassDefFoundError: className

      How could the class be there for most of the objects, but not all of them? (The list has 200 objects and only 7 cause the exception.)

      I'm using an API developed by a different group on our team. Before I go to them with this error, I want to make sure there isn't a configuration issue on my side.

      Any thoughts?

      Thanks!

      - Stephanie
        • 1. Re: java.lang.NoClassDefFoundError only occurs sometimes
          796447
          java.lang.NoClassDefFoundError: className
          Then the class named "className" isn't in your classpath at runtime. That's your configuration issue.
          • 2. Re: java.lang.NoClassDefFoundError only occurs sometimes
            807607
            Thanks for the quick response!

            The classpath was my first thought also. But why would I only get the error for some of the objects? If className wasn't in my classpath, wouldn't I get the exception for all the objects? I'm a little confused...

            Thanks,
            - Stephanie
            • 3. Re: java.lang.NoClassDefFoundError only occurs sometimes
              796447
              Of course not all classes are in the same jar or directory tree!
              Otherwise there would be no need to specify a classpath, or a classpath would not support having multiple classpath elements (multiple jars, multiple directories).

              Somewhere along the line you have not completely specified the classpath to include the class(es) in question, or have erred in the class names. Is this trying to dynamically load classes via reflection? Are you sure it fully-qualifies the class(es) by including their package names in the class name (like "com.yourcompany.YourClassName" instead of just "YourClassName")?
              • 4. Re: java.lang.NoClassDefFoundError only occurs sometimes
                807607
                Thanks again for the response!

                I'm still confused. Let me put some class names with things - maybe that will help me understand what's going on.

                I have a class called Search. A procedure inside search uses an api to query a database. The api call returns an array of CatalogSearchResults. Each CatalogSearchResults contains an AircraftPositionDAO (at least it's supposed to). The code to get the DAO is:
                try {
                   DataAccessObject dao = result.toDataAccessObject(); // exception here
                if (dao instanceof AircraftPositionDAO) {
                AircraftPositionDAO apdao = (AircraftPositionDAO) dao;
                <more code...>
                }
                }
                catch (DataAccessException err) {
                err.printStackTrace();
                }


                For most of the items in result[], the above code works. But for a couple, when I do the 'result[[i].toDataAccessObject()', java.lang.NoClassDefFoundError exception is thrown.

                Everything I'm doing should be with the same class. If it can find the class for result[0], shouldn't the same class also be there for result[1]? If the class is not there for result[1], does that mean it's not the same class as result[0]?

                Does that make any sense at all? I feel like I don't understand the problem well enough to explain it better.

                Thanks again for your help. I really appreciate it.

                - Stephanie
                • 5. Re: java.lang.NoClassDefFoundError only occurs sometimes
                  796447
                  DataAccessObject dao = result[ i ].toDataAccessObject(); // exception here
                  toDataAccessObject returns a DataAccessObject or something which extends or implements DataAccessObject.
                  So result[0].toDataAccessObject() might return a different class of object than result[1].toDataAccessObject() does. Depends on what result[0] actually is versus result[1], and the implementation of the toDataAccessObject() method. You can't just make assumptions.
                  • 6. Re: java.lang.NoClassDefFoundError only occurs sometimes
                    807607
                    So what is happening is instead of returning a DataAccessObject, it's returning something of a different class. I don't have the other class in my classpath, so that's why I get the exception. Is that correct?

                    How would I "protect" my code against that? The api says a DataAccessObject will be returned, but it sounds like something else is being returned. Since I obviously can't put every possible class in my classpath, what do I need to do? Would something like:
                    Object obj = result.toDataAccessObject();
                    if (obj instanceof DataAccessObject) {
                    DataAccessObject dao = (DataAccessObject) obj;
                    <etc.>
                    }

                    take care of it?

                    Sorry if these are dumb questions. I'm fairly new to Java.

                    Thanks,
                    - Stephanie
                    • 7. Re: java.lang.NoClassDefFoundError only occurs sometimes
                      796447
                      So what is happening is instead of returning a
                      DataAccessObject, it's returning something of a
                      different class.
                      It is returning something which is assignable to a DataAccessObject interface or base class, whichever that is.
                      I don't have the other class in my
                      classpath, so that's why I get the exception. Is
                      that correct?
                      Yes, apparantly.
                      How would I "protect" my code against that?
                      You can't other than ensure your runtime classpath has all the possible classes which may be used. At runtime the method is trying to create a class which you do not have in the classpath.
                      The api says a DataAccessObject will be returned, but it
                      sounds like something else is being returned. Since
                      I obviously can't put every possible class in my
                      classpath, what do I need to do? Would something
                      like:
                      Object obj = result.toDataAccessObject();
                      if (obj instanceof DataAccessObject) {
                      DataAccessObject dao = (DataAccessObject) obj;
                      <etc.>
                      }

                      take care of it?
                      No it would not help. It's the toDataAccessObject() method implementation which is failing, so it doesn't even return to you it appears.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           
                      • 8. Re: java.lang.NoClassDefFoundError only occurs sometimes
                        807607
                        Okay, I think I understand the problem now. Thanks so much for your help and patience! I really appreciate the time you've taken to help me out.

                        Thanks again,
                        Stephanie