20

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.

Buhake Sindi
  • 82,658
  • 26
  • 157
  • 220
Ashish
  • 1,251
  • 4
  • 15
  • 30
  • Look for the JAR file that should contain the `IWorkspaceRunnable` class or interface. Is it present in your classpath? – S.L. Barth Nov 16 '11 at 12:38
  • possible duplicate of [When to catch java.lang.Error?](http://stackoverflow.com/questions/352780/when-to-catch-java-lang-error) – kv-prajapati Nov 16 '11 at 12:39
  • 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... – aishwarya Nov 16 '11 at 12:40

2 Answers2

68

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?

Tomasz Nurkiewicz
  • 311,858
  • 65
  • 665
  • 652
  • 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. – Mecki Jan 08 '15 at 17:49
  • 1
    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.? – ACV Jul 20 '15 at 09:16
  • 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 it – Luke Aug 02 '16 at 14:29
  • @ACV Thanks for explaining why I'm getting this error! – Noumenon Dec 04 '16 at 12:50
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.

Buhake Sindi
  • 82,658
  • 26
  • 157
  • 220
  • 26
    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. – Mecki Jan 08 '15 at 17:48
  • 5
    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](http://english.stackexchange.com/questions/56065/must-not-or-may-not-which-is-the-most-correct)). – Buhake Sindi Jan 09 '15 at 09:04
  • 2
    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__. – Mecki Jan 09 '15 at 11:01
  • 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. – Buhake Sindi Jan 09 '15 at 11:07
  • 1
    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. – Mecki Jan 09 '15 at 13:40
  • 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. – Buhake Sindi Jan 09 '15 at 14:01
  • 1
    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. – Mecki Jan 13 '15 at 09:52
  • 1
    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. – Mecki Jan 13 '15 at 09:56
  • 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. – Buhake Sindi Jan 13 '15 at 12:08
  • @Mecki 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. :D – peterchaula Apr 09 '17 at 13:26
  • `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 CLASSPATH** – a.l. Mar 29 '21 at 03:55