Reputation: 10549
I have this test class
import javax.xml.bind.annotation.XmlElement;
class CompileTest {
void foo( @XmlElement String in ) {
}
}
my java version is
$ java -version
java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) Client VM (build 19.0-b09, mixed mode, sharing)
and when I try to compile that class I'm getting
javac CompileTest.java
CompileTest.java:5: annotation type not applicable to this kind of declaration
void foo( @XmlElement String in ) {
^
1 error
and that's valid for Java 6. When I tried to add newer JAXB library to class path, it didn't help. Is there a way to solve this?
javac -cp jaxb-api-2.2.4.jar CompileTest.java
Upvotes: 10
Views: 8803
Reputation: 3124
Use the concept of "endorsed libraries" folder. Take a look here: How can I make Ant use JAXB x instead of Java 6 SE JAXB classes ...
Basically this is a way to instruct the JRE to use a more recent version of JAXB.
You can read more here: Unofficial guide to JAXB: Using JAXB 2 with SE 6 Also see this question: What is the exact way to use endorsed directory in JRE 6
Upvotes: 4
Reputation: 7692
It can only be applied at field or method, not on method parameter. See
@Retention(RUNTIME) @Target({FIELD, METHOD})
public @interface XmlElement {
Edit: Source of XmlElement (JDK 1.6.0_18)
* @since JAXB2.0
* @version $Revision: 1.19 $
*/
@Retention(RUNTIME) @Target({FIELD, METHOD})
public @interface XmlElement {
So what I see is 1.6.0_18 version has XmlElement of 1.19 revision having only FIELD
and METHOD
target available.
Edit: So your problem is jdk 1.6 XmlElement isn't same as jaxb.2.2. you can check source at http://grepcode.com/file/repo1.maven.org/maven2/javax.xml.bind/jaxb-api/2.2.4/javax/xml/bind/annotation/XmlElement.java?av=f
Upvotes: 2
Reputation: 51711
Use the Java Endorsed Standards Override Mechanism
Put your jaxb-api-2.2.4.jar
inside <java-home>\lib\endorsed
directory.
Or, use the -D java.endorsed.dirs option
javac -Djava.endorsed.dirs=/your/path/to/jaxb-directory CompileTest.java
References:
http://docs.oracle.com/javase/6/docs/technotes/guides/standards/
Upvotes: 11