Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 599
Ashish Avatar asked Nov 16 '11 12:11

Ashish


People also ask

Can we catch NoClassDefFoundError in Java?

In the case of NoClassDefFoundError, the class was present at compile time, but Java runtime could not find it in Java classpath during runtime.

How can I solve Java Lang NoClassDefFoundError?

lang. NoClassDefFoundError, which means the Class Loader file responsible for dynamically loading classes can not find the . class file. So to remove this error, you should set your classpath to the location where your Class Loader is present.

Can we catch ClassNotFoundException in Java?

ClassNotFoundException is a checked exception, so it has to be catch or thrown to the caller. ClassNotFoundException always occurs at runtime because we are indirectly loading the class using Classloader. Java compiler has no way to know if the class will be present in the classpath at runtime or not.


2 Answers

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?

like image 88
Tomasz Nurkiewicz Avatar answered Oct 16 '22 17:10

Tomasz Nurkiewicz


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.

like image 24
Buhake Sindi Avatar answered Oct 16 '22 17:10

Buhake Sindi