java.lang.NoClassDefFoundError thrown for static method but not for static member
Вопрос
Working with the dnsjava library, I've encountered a frustrating and confusing problem where I can call Type.A
properly but calling Type.value(str)
throws a java.lang.NoClassDefFoundError
.
System.out.println(org.xbill.DNS.Type.A); // works
if (org.xbill.DNS.Type.value(type) == -1) { // throws NoClassDefFoundError
/* logic */
}
This code is being executed from a jar
and other classes in the jar use the library correctly.
Why and how could this be happening? How can I debug this further?
Thanks!
EDIT
Jon Skeet was correct. A friend showed me how to use javap -c
and I changed the value to something more distinct, Type.AAAA, which has a value of 28:
878: getstatic #116; //Field java/lang/System.out:Ljava/io/PrintStream;
881: bipush 28
Решение
It sounds like you're missing the dnsjava
library at execution time.
That doesn't matter for Type.A
because that's a constant - the value is being pulled out by the compiler and embedded directly into your code, as if you'd specified it as an integer literal. It doesn't need the library to be present at execution time. That's clearly not the case when you call a method though.
Другие советы
You are probably missing the Type
class on your runtime classpath. You need to run the jar with -cp
argument.
What is that value of type
when the runtime exception is thrown? if you read the java doc it Converts a String representation of an Type into its numeric value
. It probably does this by instantiating it reflectively or some muck. If you run this method for a type string for which there is no associated Type
I think its perfectly reasonable for you to get this error.
Step 1: print the string type and make sure a Class of that type is on the classpath.