1
votes

I have the following code. After compilation I delete the MyClassToLoad.class file and run the code.

public class ClassLoadersTest {
    public static void main(String[] args) {
        MyClassToLoad c = new MyClassToLoad();
    }
}

I get the following stacktrace:

Exception in thread "main" java.lang.NoClassDefFoundError: classloaders/MyClassToLoad at classloaders.ClassLoadersTest.main(ClassLoadersTest.java:9) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

Caused by: java.lang.ClassNotFoundException: classloaders.MyClassToLoad at java.net.URLClassLoader$1.run(URLClassLoader.java:200) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:188) at java.lang.ClassLoader.loadClass(ClassLoader.java:303) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at java.lang.ClassLoader.loadClass(ClassLoader.java:248) at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
... 6 more

As far as I understand, this stacktrace means that there was a ClassNotFoundException, that was caught and rethrown as a NoClassDefFoundError.

The questions are:

1) How can I understand at which line the rethrow happened?

2) Who cuts the stacktrace with "... 6 more" - Java or Idea? How can I see it full?

3) As far as I understand to force the rethrown exception contain the full stacktrace we need to rethrow it as

throw new SomeRethrownException("some text", exceptionWhichIsTheReason)

But the NoClassDefFoundError doesn't have such a constructor. So in fact it shouldn't print the full stacktrace.. or may be they just put it as Error message as a String?

1
The "... 6 more" entries in the ClassNotFoundException stack trace are exactly the six entries in the NoClassDefFoundError trace. - Stuart Marks
Stuart, it is ALWAYS like this? - MiamiBeach
Generally yes, the truncated entries are usually the same as those of the enclosing exception. See Throwable.printStackTrace. - Stuart Marks

1 Answers

0
votes
  1. classloaders.ClassLoadersTest.main(ClassLoadersTest.java:9) at <-- here

  2. The runtime system. Try

    public static void main(String[] args) {
      try {
        MyClassToLoad c = new MyClassToLoad();
      } catch (java.lang.NoClassDefFoundErro e) {
        e.getCause().printStackTrace();
      }
    }
    

As for 3, see my answer to 2.