This discussion is archived
9 Replies Latest reply: Nov 11, 2009 1:00 AM by 791266 RSS

Custom Annotation showing Error: is missing <clinit>

843793 Newbie
Currently Being Moderated
Hi!
I have created some custom annotations for use in my project. Sometimes they show error: is missing <clinit> and sometimes they don't. I'm confused. If I create a jar and use that jar in another project this error appears at compiletime .

Here is the source of one of my custom annotations....

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* User: yogeshd
* Date: Mar 30, 2007
* Time: 10:54:31 AM
*/
@Constraint
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ConstraintMaxIntValue {

public Validator validator = new MaxIntValidator();

int value() default 0;

}

The strange thing is that this error doesnot appears all the time...

If anyone can explain the reason I will be enlightened. ;) thnx.
  • 1. Re: Custom Annotation showing Error: is missing <clinit>
    843793 Newbie
    Currently Being Moderated
    The line
    public Validator validator = new MaxIntValidator();
    declares a field in the annotation type rather than a method. While it is legal to declare fields in annotations (and interfaces in general), it is not recommend. If such a field is present, a static initializer (clinit) in the annotation type is used to initialize the field when the type is loaded.
  • 2. Re: Custom Annotation showing Error: is missing <clinit>
    843793 Newbie
    Currently Being Moderated
    So what actually is wrong with this code. And why does the error appears only some times and why it says is missing <clinit>.

    Please explain..........
  • 3. Re: Custom Annotation showing Error: is missing <clinit>
    843793 Newbie
    Currently Being Moderated
    So what actually is wrong with this code. And why
    does the error appears only some times and why it
    says is missing <clinit>.

    Please explain..........
    I don't know why you get the error intermittently. You haven't provided any information about what environment and compiler you are using.
  • 4. Re: Custom Annotation showing Error: is missing <clinit>
    843793 Newbie
    Currently Being Moderated
    This Error appears in the class where I use these annotations not in the annotation class itself.

    I'm using jdk 1.5.0-rc I even tried it with jdk 1.6.0-beta2 but recieved the same error.
    My Project Language level is 5.0, if it matters.

    I could use an alternate way by not declaring the Validator field in Annotation but I am just wondering what the reason for this error might be........

    And again..... I get the same error if I use a jar file of this compiled code in another project..... :)

    Message was edited by:
    yogeshd
  • 5. Re: Custom Annotation showing Error: is missing <clinit>
    608410 Newbie
    Currently Being Moderated
    yogeshd,

    It might be that the .class file for the annotation that you are compiling against is the one you have given us the source code for above, but when you run the program, the class file for the annotation is not the same, and is one that was compiled before you added the static field.

    This can happen if your classpath is wrong. (and would explain why the problem only occurs sometimes - because it is only failing when the classpath is wrong).

    If you run the following program with the Fully qualified name of your annotation as the argument, and with the same classpath as you run your program, it can tell you where it is finding the annotation class file. Check that this is the same one that you are compiling to.
    class WhereLoaded {
        public static void main(String[] args) {
            Class theClass=null;
            if(args.length > 0) {
                try {
                    theClass=Class.forName(args[0]);
                } catch (Exception e) {
                    e.printStackTrace();
                    theClass=Object.class;
                }
            } else {
                System.out.println(
                    "The first argument should be a fully qualified class name," +
                    " using java.lang.Object instead"
                );
                theClass=Object.class;
            }
            String name=theClass.getName();
            name=name.substring(name.lastIndexOf(".")+1) + ".class";
            System.out.println(name + " loaded from " + theClass.getResource(name));
        }
    }
    regards

    Bruce
  • 6. Re: Custom Annotation showing Error: is missing <clinit>
    843793 Newbie
    Currently Being Moderated
    I checked bruce...but the class file is updated....
    Notice that I mentioned that this error appears only some times and these occure in the Java file where I use my annotations...
    Some times they compile and run....If I make a change in the java file where I am using the annotation I have to re-build the entire project to get the effect...not just the java file where I am using the annotation...otherwise I get the same error....Missing <clinit>

    And I have tested this project in different professional java ide's like netbeans and eclipse.

    It's strange I haven't got a satisfactory answer yet.... :(
  • 7. Re: Custom Annotation showing Error: is missing <clinit>
    843793 Newbie
    Currently Being Moderated
    Yes, I think Bruce is right. I ran into the same error. I was having no issues in eclipse, but had errors building with ant. I was adding old class files to / of the imported jar (containing the annotations) and mis-adding the new within a prefix dir, causing the old to be picked up and the <clinit> error to occur when compiling with ant. It is a subtle problem, and I suggest tracking down the jar file containing the annotation which is causing problems and comparing the class file size in archive with the known good class file size to help to ensure they're the same class. ... yes, and this is a year later, but this confirmation and advice may be useful to somebody.
  • 8. Re: Custom Annotation showing Error: is missing <clinit>
    843793 Newbie
    Currently Being Moderated
    Again, over a year has passed since the last reply, but I hit the same problem. I have a custom annotation and sometimes javac just refuses to see it. It gets compiled to .class but some (not all) uses of it are flaged as errors.

    It's not (at least in my case) about wrong class or jar. I had a public static final constant defined within the annotation, and when I remove that, everything is fine again.

    I can repeat this at least with MacOS X 10.5.8, JDK 1.6.0_15-b03-226 and Ubuntu 9.10 i386 server, 1.6.0_17-b04.
  • 9. Re: Custom Annotation showing Error: is missing <clinit>
    791266 Explorer
    Currently Being Moderated
    >

    Hi,

    Please, don't resurrect old threads. I'm locking this one. Create a new thread if you have a specific question.

    Kaj