10

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
bdoughan
  • 147,609
  • 23
  • 300
  • 400
Betlista
  • 10,327
  • 13
  • 69
  • 110
  • The error is from the compiler, in effect, from a different (newer) version of the language. So replacing the jar will have no effect. Just curious, why don't you use Java 6? – talonx Apr 25 '13 at 13:03
  • I do not understand. "...from a different (newer) version of the language...", what? "Just curious, why don't you use Java 6?" I am, maybe it should be Java 7, it's because we cannot migrate from 6 to 7, while there is Java 6 in production actually... – Betlista Apr 25 '13 at 13:11
  • @Betlista - Similar question and a solution is at http://stackoverflow.com/questions/8211420/xmlelement-annotation-dissallowed-with-webparam – Andy Thomas Apr 25 '13 at 13:11

3 Answers3

11

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/

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • I like the java.endorsed.dirs parameter, because I do not need to modify java installation for all applications... – Betlista Apr 25 '13 at 14:34
4

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

Community
  • 1
  • 1
GrahamMc
  • 3,034
  • 2
  • 24
  • 29
2

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

harsh
  • 7,502
  • 3
  • 31
  • 32
  • As I wrote "...that's valid for Java 6...", "...I tried to add newer JAXB library to class path...", where target is `@Target({FIELD, METHOD, PARAMETER})`. – Betlista Apr 25 '13 at 13:04
  • @harsh - In Java 6, it's declared @Target(value={FIELD,METHOD,PARAMETER}). See http://docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/XmlElement.html – Andy Thomas Apr 25 '13 at 13:06
  • That's exactly why am I asking how to use the class from `jaxb-api-2.2.4.jar` with Java 6 if it's even possible. – Betlista Apr 25 '13 at 13:08
  • ok, so when I look at the source of `rt.jar` version `java version "1.6.0_18"`, it shows only FIELD and METHOD. Edited my answer to include version information on XMLElement as well. Can you look at the source of jaxb-api-2.2.4.jar? your error reflects that its picking XMLElement from rt.jar instead of what you are intending. – harsh Apr 25 '13 at 13:16