简体   繁体   English

是否有可能在Java JRE 1.6 SE中替换JAXB实现的版本?

[英]Is it possible to replace the version of the JAXB implementation in Java JRE 1.6 SE?

I have this test class 我有这个测试课

import javax.xml.bind.annotation.XmlElement;

class CompileTest {

   void foo( @XmlElement String in ) {
   }

}

my java version is 我的java版本是

$ 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. 这对Java 6有效。当我尝试将更新的JAXB库添加到类路径时,它没有帮助。 Is there a way to solve this? 有办法解决这个问题吗?

javac -cp jaxb-api-2.2.4.jar CompileTest.java

Use the Java Endorsed Standards Override Mechanism 使用Java认可的标准覆盖机制

Put your jaxb-api-2.2.4.jar inside <java-home>\\lib\\endorsed directory. jaxb-api-2.2.4.jar放在<java-home>\\lib\\endorsed目录中。

Or, use the -D java.endorsed.dirs option 或者,使用-D java.endorsed.dirs选项

javac -Djava.endorsed.dirs=/your/path/to/jaxb-directory CompileTest.java

References: 参考文献:
http://docs.oracle.com/javase/6/docs/technotes/guides/standards/ http://docs.oracle.com/javase/6/docs/technotes/guides/standards/

Use the concept of "endorsed libraries" folder. 使用“endorsed libraries”文件夹的概念。 Take a look here: How can I make Ant use JAXB x instead of Java 6 SE JAXB classes ... 看看这里: 我如何让Ant使用JAXB x而不是Java 6 SE JAXB类......

Basically this is a way to instruct the JRE to use a more recent version of JAXB. 基本上这是一种指示JRE使用更新版本的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 您可以在这里阅读更多内容: JAXB的非官方指南:将JAXB 2与SE 6一起使用另请参阅此问题: 在JRE 6中使用已签名目录的确切方法是什么

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) 编辑: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. 所以我看到1.6.0_18版本的XmlElement为1.19版本,只有FIELDMETHOD目标可用。

Edit: So your problem is jdk 1.6 XmlElement isn't same as jaxb.2.2. 编辑:所以你的问题是jdk 1.6 XmlElement与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 您可以访问http://grepcode.com/file/repo1.maven.org/maven2/javax.xml.bind/jaxb-api/2.2.4/javax/xml/bind/annotation/XmlElement.java?av查看来源= F

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM