Why am I getting a ClassCastException when generating javadocs?

I have some idea regarding this problem but this not exact solution to get.

If you give single comment line // before annotation and try to run the javadoc once again. This problem will solve

Eg: sample.java file

@ChannelPipeline

Makes changes in

//@ChannelPipeline

Try to run javadoc command once again. Now ClassCastException won't occur.


It looks like this has been reported as a Java bug. It appears to be caused by using annotations from a 3rd party library (like JUnit) and not including the jar with that annotation in the javadoc invocation.

If that is the case, just use the -classpath option on javadoc and include the extra jar files.


There is another way to get a ClassCastException in versions of Java from 5 through 8:

java.lang.ClassCastException: com.sun.tools.javadoc.MethodDocImpl cannot be cast to com.sun.tools.javadoc.AnnotationTypeElementDocImpl

It will happen when javadoc encounters a reference to a annotation method in javadoc text before processing the same annotation for the first time used in code. Take these two classes:

/**
 ** {@link javax.annotation.Generated#value()}
 */
public class TestClass1 {}


@Generated("sometext")
public class TestClass2 {}

The bug is order dependent. If javadoc processes TestClass1 first, the ClassCastException will be thrown. If javadoc processes TestClass2 first, it will complete fine. The bug is reported as JDK-8170444, and was resolved as "Won't Fix". The bug is no longer present in Java 9.

As a workaround, don't link to annotation methods in your documentation text.