How to catch java.lang.NoClassDefFoundError?
Asked Answered
C

3

26

I made an application which take elf file(*.a and *.o) and give list of methods name, but if someone renames any file into *.a or *.o then it will show:

Exception occurred during event dispatching:
java.lang.NoClassDefFoundError: org/eclipse/core/resources/IWorkspaceRunnable
    at org.eclipse.cdt.utils.AR.<init>(AR.java:237)
    at com.lge.windowELF.ElfBinaryArchive.<init>(ElfBinaryArchive.java:24)
    at com.lge.windowELF.ELFParserLibraryFile.createBinaryArchive(ELFParserLibraryFile.java:230)
    at com.lge.windowELF.ELFParserLibraryFile.<init>(ELFParserLibraryFile.java:46)
    at com.lge.windowELF.ELFWrapper.<init>(ELFWrapper.java:36)
    at com.lge.windowELF.ELF_UIIntegrated.actionPerformed(ELF_UIIntegrated.java:510)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(Unknown Source)

In this situation I want to give some warning message. This exception is not caught by try/catch.

Carisacarissa answered 16/11, 2011 at 12:33 Comment(3)
Look for the JAR file that should contain the IWorkspaceRunnable class or interface. Is it present in your classpath?Parasympathetic
possible duplicate of When to catch java.lang.Error?Ion
it can be caught by catch this specific error or Throwable. but may be you should handle the missing file condition in your code more elegantly...Oxcart
G
79

NoClassDefFoundError is a subclass of Error and not an Exception. Hence you need to use:

try {
  new org.eclipse.cdt.utils.AR();
}
catch(NoClassDefFoundError e) {
  //handle carefully
}

in your code. Note that you shouldn't ever catch Error or Throwable. Also make sure that you surround as little code as possible with this catch as this exception should not typically by caught.

UPDATE: Also are you sure you want to catch this exception? It is very rare and I can't imagine how do you want to handle it. Maybe you should just add a JAR with IWorkspaceRunnable class to your CLASSPATH?

Gloze answered 16/11, 2011 at 12:37 Comment(5)
This answer is much better, it should be the accepted one as it actually really answers the question and at the same time helps to solve the real problem.Spelling
Could be a situation when you might want to catch it. When you redeploy the application in a web app container. Then there is a session destroy listener and there it tries to find a class that is not on the classpath anymore because it has been undeployed.?Sunstone
here's a situation: i have the required jar in my classpath but I am distributing the code to people who may not and want to ensure they know how to add it when they try to run itUzial
@Sunstone Thanks for explaining why I'm getting this error!Mokas
It's useful in OSGI / karaf if, for some reason, your bundle is removed or deactivated. But yes, handle with care.Singspiel
A
5

It's not encouraged to catch an Error! JavaDoc states:

An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch.

My suggestion is that you sort out the NoClassDefFoundError and worry about exceptions thrown by your code instead.

I would rather, in code, throw an InvalidELFFileException (educated guess) on ElfBinaryArchive constructor class (or wrap the class and do a throws when instantiating) when the class tries to open the ELF file. That way, if there's an invalid ELF file, a decent exception is thrown.

Alternatively, make sure org.eclipse.core.resources.IWorkspaceRunnable must be put in CLASSPATH.

Angeles answered 16/11, 2011 at 12:38 Comment(12)
Downvote because should not is not the same as must not or never, should not means it is allowed, just not recommended. And if someone asks "How an I do something?", it is generally not helpful to say "Just don't do it"; I'd even consider that rude, even if your answer may have been helpful.Spelling
I never knew we're downvoting based on the use of the English language. I thought SO was based on programming related solutions. If this was based on the english language then shouldn't this post be more beneficial on English StackExchange? (See example).Angeles
A user asks you how to do something and you tell him he shall not do it, that's why you got downvoted. You shall not do it is no answer to the question.Spelling
Not true. I told the OP what should not be done at all AND I provided a recommendation/suggest/response that satisfies/answers his/her question.Angeles
Telling him it should not be done is no answer to the question how it should be done. The question (still) is: How to catch java.lang.NoClassDefFoundError? Where exactly have you answered that? You haven't. If I didn't know how to do that, I still wouldn't know it after reading your answer.Spelling
If you see my recommendation from I would rather, in code..... then you would know that I did specify a recommendation. Also, you would see the last paragraph starting with the word Alternatively. It's not that I have added this today. Maybe you're just reading the first 2 paragraphs. There are other things mentioned on my answer.Angeles
You can argue with me all day long, it won't change the facts. You have not answered the question; period. No part of your answer shows how to catch that exception and that was the question; period. Tomasz Nurkiewicz has answered the question, showing a try block that catches the exception (THAT is the correct answer to the question, nothing else is). Flagged your answer as "Not an answer". And now stop this stupid discussion, it's a waste of time as I won't take back the downvote; trying to be helpful is one thing, but answering a question is another thing.Spelling
I came here to find an answer to exactly that question, I needed to catch exactly this exception and reading your answer was just a waste of time. I didn't have a classpath problem and I didn't want to sort out any problem because that the class may not exist is intended behavior. I just wanted to catch that error and you didn't tell me how to do that, thus this is NOT AN ANSWER TO THE QUESTION. End of discussion.Spelling
It may not be an answer to you but it helped the OP. I didn't ask you to retract your downvote. Keep it. These comment may be beneficial to those who have an open mind like yours.Angeles
@Spelling my opinion is that responses don't always have to be direct answers to the question. If you take a look at the title on the upvote button it says "This answer is useful". If you think this response is helpful, then you owe the author an upvote. :DCornute
NoClassDefFoundError does not means class not found on the classpath, but any exception occurs during its static init phase, so it may not be benefit to make sure org.eclipse.core.resources.IWorkspaceRunnable must be put in CLASSPATHElectrograph
"I never knew we're downvoting based on the use of the English language." you could argue that point about ANY answer given in English that you don't like.Chemo
C
0

I just wanted to add an answer in response to it not being recommended to catch this error. I think it's fine in the right situation and if done in the right way. I can think of several situations where it makes sense

  • if your application relies on an end user supplying some of the dependencies
  • some of the dependencies are pre-installed by another application
  • you just want to give a cleaner error message and exit

For me this is very common, I supply an application and the OPs team supply the environment. If certain dependencies don't exist I log an error saying that functionality does not exist but my application continues to run.

I would also add that you need to make sure within the catch block that the dependencies you use there have been confirmed to exist. This isn't that hard, if you need a jar for logging for example and you've used it previously in your code with success then you're all good. If the logging dependency didn't exist then it shouldn't be possible for your code to get that far anyway.

Chemo answered 11/2, 2022 at 2:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.