A few days ago, I've tried to solve this SO-Question.
I know about static initializers and there benefits in java
. But due to the problem I was thinking if they could be the wrong design decision if they reference a class outside of the jar.
What happened in that question?
The user tried to use CXF
in his environment and failed with -
Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.cxf.common.injection.ResourceInjector
The problem was very suspicious to me, so I asked him to add an example and I have tried to debug his example, but every time in eclipse when I was at the code where ResourceInjector
was loaded, I ended up with MethodInvocation
... Exception. I didn't understand the problem.
Then, another user pointed out that there is a static initializer in the cxf
class:
static {
ANNOTATIONS.add(Resource.class);
ANNOTATIONS.add(Resources.class);
}
Resource(s).class is from the javax.annotation package, which couldn't be found in the environment.
I think to find the problem in this issue was kinda hard, and I also think most developers would have problems to find the problem (including me).
Are static initializers with references to other jars a bad design decision?