Search code examples
javaexceptionruntime-errornoclassdeffounderror

Handle a "NoClassDefFoundError"?


Is there a way I could ignore a java.lang.NoClassDefFoundError like try catch where it calls the missing class? I dont need to fix the missing classes because this is part of my program.


Solution

  • I dont need to fix the missing classes because this is part of my program.

    I am not completely sure what you are trying to achieve here but you should be able to catch a NoClassDefFoundError to prevent your JVM from crashing. Let A be a class that has-a reference to B. If B.class is not available at runtime, you can handle the NoClassDefFoundError as follows :

    class C {
        public static void main(String args[]) {
            try {
                A a = new A();
            } catch (NoClassDefFoundError e) {
                //log the error or take some action
            }
    
            System.out.println("All good here, lets continue");
        }
    }