1

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
2
  • Does adding the -verbose:class command line argument reveal something? Often when you get this error, it's a different class that is missing, one that is referenced by the class you want to load. Commented Mar 2, 2012 at 22:05
  • Could you show the full stacktrace of the NoClassDefFoundError? This error is thrown in at least three different situations. Commented Mar 2, 2012 at 22:05

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

You're right, Type.A is an int and therefore the value can be pulled straight in. Just the fact that it's a constant isn't enough, if it was a constant of type Type, it wouldn't work.
@biziclop: It wouldn't be a constant in JLS terminology in that case :) "A variable of primitive type or type String, that is final and initialized with a compile-time constant expression (§15.28), is called a constant variable."
@Jon didn't OP say that other classes are using parts of this library correctly at runtime?
@nsfyn55: I'd missed that bit - but I suspect it's not true :) (Alternatively, they may be using some older version or something like that.)
@JonSkeet Thanks, I'm sure this is exactly what is happening.
0

You are probably missing the Type class on your runtime classpath. You need to run the jar with -cp argument.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.