GeeCON Prague 2014*
The moose likes Java in General and the fly likes Suddenly getting java.lang.NoClassDefFoundError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Suddenly getting java.lang.NoClassDefFoundError " Watch "Suddenly getting java.lang.NoClassDefFoundError " New topic
Author

Suddenly getting java.lang.NoClassDefFoundError

Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
Dear Java Gurus and Experts,

I need your help and suggestions on a strange issue I am facing. In my project sometime I am getting java.lang.NoClassDefFoundError

followded by java.lang.ClassNotFoundException

Though this Error is not consistant and not reproducable so far, below is the full stack of the Error, [ replaced the real class with com/abc/bcd/efg/ABC }


java.lang.NoClassDefFoundError: com/abc/bcd/efg/ABC
at aa.bb.cc.dd.ee.ff.ABC.BCD.TEST$TESTSubscription.processingSomething(TEST.java:502)
at aa.bb.cc.dd.ee.ff.ABC.BCD.TEST$TESTSubscription.processUpdate(TEST.java:363)
at aa.bb.cc.dd.ee.ff.ABC.BCD.TEST$TESTSubscription.processInsert(TEST.java:327)
at A.B.C.D.AbstractDataCallback.update(AbstractDataCallback.java:26)
at A.B.C.D.DefaultRawDataCallback.update(DefaultRawDataCallback.java:64)
at A.B.C.D.ServiceHandle.processData(Service.java:804)
at A.B.C.D.ServiceHandle.listen(Service.java:747)
at A.B.C.D.ServiceHandle.getSubs(Service.java:558)
at A.B.C.D.ServiceHandle.getSubs(Service.java:508)
at aa.bb.cc.dd.ee.ff.ABC.BCD.TEST$TESTSubscription$1.run(TEST.java:1091)
Caused by: java.lang.ClassNotFoundException: com/abc/bcd/efg/ABC
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)


This is surely a very strange issue, as this is not happening every time.



Thanks in advance,

With Regards,
Damodar


SCJP 1.5, SCWCD 1.4, SCBCD 5.0, next SCDJWS...
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Damodar Mukhopadhyay wrote:
This is surely a very strange issue, as this is not happening every time.

I would think this is thrown when certain piece of code is executed, which would be consistent when the required classes would be tried to be loaded.
This missing ABC class, is it in some jar? Is it on the class path?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
check your classpath. Mostly as maneesh pointed out, some jar has gone missing. Or probably you renamed some class. You might also want to check the import statements. I have had cases where the eclipse auto import messed up my actual imports
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10426
    
    8

Gaurav Raje wrote:You might also want to check the import statements. I have had cases where the eclipse auto import messed up my actual imports

In that case, the code wouldn't compile in the first place, not throw an exception at runtime
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
The classpath is fine also the class is is the jar.
The problem is not consistant, it not happening every time I run it!


Sometime the JVM is not able to find that class!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

The class path is fine during compile time, but during runtime it's obviously missing at least one class.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Gaurav Raje
Ranch Hand

Joined: Jul 23, 2010
Posts: 136
Maneesh Godbole wrote:
Gaurav Raje wrote:You might also want to check the import statements. I have had cases where the eclipse auto import messed up my actual imports

In that case, the code wouldn't compile in the first place, not throw an exception at runtime

Sorry my bad for explaining it poorly.
I was talking in particular about eclipse. Suppose if you are using a class called Date(strictly an arbitrary example) . which is present in both, java.sql.Date as well as java.util.Date. So while compile time, there are cases where, eclipse in its infinite wisdom converts the import statements from one to the other. So if you have a
Date d = new Date() // expecting to use java.util.Date, sometimes the import statement is converted to java.sql.date

Now if you have java.sql.Date on your classpath in eclipse, it shows no compilation error. And you have no idea that in reality you need the java.sql.Date on your classpath. But when you run it, without having java.sql.Date on your classpath, you get the noClassDefFoundError.
You doublecheck to ensure that java.util.Date is present.

May be it was just me who faced this issue, but its more of an IDE fault than anything else.
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
Thanks, I understood your explanation.
The surprising thing is, the program is not failing every time.

Its running properly, but some time it fails due to the Error!

Every time the classpath is the same.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Damodar Mukhopadhyay wrote:
Its running properly, but some time it fails due to the Error!

Every time the classpath is the same.


Where are you running it eclipse? Web-app? Rerunning on the same machine gives you the issue? More details would help ...


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
I faced a similar issue in Eclipse before. Try closing the project, close Eclipse and reopen the project. It luckily did fix the 'no class found' error.
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
Thanks john, but I am not running the project from Eclipse, its running in a linux server!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Oh you compile the classes using Eclipse and generating a jar and running that in linux? or something else?
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
Yes John...
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
I create the jar in Eclipse and run it in linux.
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
Okay. Please check if manifest file is having class path set to all referenced jars. Ignore if you set class path else where (say the ksh where you execute the java cmd)
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
I set classpath in script.

Also if I get this error and run it again it runs fine!!!
John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
MAGIC
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
yes.. its really a surprise!!!
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

Is the com/abc/bcd/efg/ABC in a different jar file than the one which is using it - at aa.bb.cc.dd.ee.ff.ABC.BCD.TEST$TESTSubscription.processingSomething(TEST.java:502)

Is there a pattern in the failure? Like it always fails the first time and works the next time?
Does it always work when you run it from your IDE?

Error like NoClassDefFoundError are always a bit tricky to understand specially when they happen inconsistently... but the reason for NoClassDefFoundError this is that its not finding the class on the classpath
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
Its in a same and single jar, other supportive jars to that project are in classpath.

No such pattern found, but when that particular class called first time the Error comes. [and not every time actually]



Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

are you by any chance using reflection to load any class?
, but when that particular class called first time the Error comes. [and not every time actually]


Also the way you say that gives the impression that your program continues to work even after getting the error ... does it? You handle that condition in code?
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
There are reflections, but the class I am getting Error is created directly from the program it self.

And NO.. let me explain please,
Say a.b.c.d.TheClass is called from a.b.c.e.MainClass where sometime I am getting the Error.

After this error the JVM goes down, and when I restart it, all work fine!!!


Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
I observed that, if the JVM is running and I redeploy the jar, and then if the JVM requires to load a class, its throwing this Error.

Is this an expected behaviour?
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

What do you mean by re-deploy the jar when the JVM is running? How do you do that?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Servers like Apache Tomcat allow you to overwrite a JAR file with a new version. That doesn't mean the new JAR file will automatically be used though, you still need to restart Tomcat.
Damodar Mukhopadhyay
Ranch Hand

Joined: Jan 15, 2007
Posts: 98
@Sam
When the JVM is already running and for some time, I deployed a new version that jar.

@Rob
Thank you so much for your advice. Noted it.
 
GeeCON Prague 2014
 
subject: Suddenly getting java.lang.NoClassDefFoundError