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
kwarrick
  • 5,930
  • 2
  • 26
  • 22
  • 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. – biziclop Mar 02 '12 at 22:05
  • Could you show the full stacktrace of the NoClassDefFoundError? This error is thrown in at least three different situations. – Luke Woodward Mar 02 '12 at 22:05

3 Answers3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 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 Mar 02 '12 at 22:23
  • @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 Skeet Mar 02 '12 at 22:25
  • @Jon didn't OP say that other classes are using parts of this library correctly at runtime? – nsfyn55 Mar 02 '12 at 22:31
  • @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.) – Jon Skeet Mar 02 '12 at 22:37
  • @JonSkeet Thanks, I'm sure this is exactly what is happening. – kwarrick Mar 02 '12 at 22:42
0

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
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.

nsfyn55
  • 14,875
  • 8
  • 50
  • 77