Is it possible to replace the version of the JAXB implementation in Java JRE 1.6 SE?
Asked Answered
E

3

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
Estey answered 25/4, 2013 at 12:59 Comment(3)
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?Bedspread
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...Estey
@Estey - Similar question and a solution is at #8211920Jenna
F
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/

Fistulous answered 25/4, 2013 at 13:20 Comment(1)
I like the java.endorsed.dirs parameter, because I do not need to modify java installation for all applications...Estey
C
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

Confrere answered 25/4, 2013 at 13:14 Comment(1)
I think Ravi's answer is better written than mine - please rather vote up @Ravi - I did!Confrere
T
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

Thriller answered 25/4, 2013 at 13:2 Comment(4)
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}).Estey
@Thriller - In Java 6, it's declared @Target(value={FIELD,METHOD,PARAMETER}). See docs.oracle.com/javaee/6/api/javax/xml/bind/annotation/…Jenna
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.Estey
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.Thriller

© 2022 - 2024 — McMap. All rights reserved.