Ashish
Ashish

Reputation: 1567

How to catch java.lang.NoClassDefFoundError?

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.

Upvotes: 27

Views: 31941

Answers (3)

MikeKulls
MikeKulls

Reputation: 1016

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.

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89169

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.

Upvotes: 5

Tomasz Nurkiewicz
Tomasz Nurkiewicz

Reputation: 340723

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?

Upvotes: 79

Related Questions