【问题标题】:Access restriction on jdk1.7/jre/lib/rt.jarjdk1.7/jre/lib/rt.jar的访问限制
【发布时间】:2012-05-12 19:50:26
【问题描述】:

大家好,我在创建 JAXB 解析器时遇到了一个非常奇怪的问题。当我尝试从 Eclipse 生成 JAXB 类时,在一个类中它显示了一个非常奇怪的错误,即

Access restriction: The type QName is not accessible due to restriction on required library /usr/lib/jvm/jdk1.7.0_02/jre/lib/rt.jar

这是我的课

package generated;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
//import javax.xml.namespace.QName;
import javax.xml.namespace.*;


@XmlRegistry
public class ObjectFactory {

在这条线上我收到了错误消息

private final static QName _ExpenseReport_QNAME = new QName("", "expenseReport");

    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: generated
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link ExpenseT }
     * 
     */
    public ExpenseT createExpenseT() {
        return new ExpenseT();
    }

    /**
     * Create an instance of {@link UserT }
     * 
     */
    public UserT createUserT() {
        return new UserT();
    }

    /**
     * Create an instance of {@link ItemT }
     * 
     */
    public ItemT createItemT() {
        return new ItemT();
    }

    /**
     * Create an instance of {@link ItemListT }
     * 
     */
    public ItemListT createItemListT() {
        return new ItemListT();
    }

    /**
     * Create an instance of {@link JAXBElement }{@code <}{@link ExpenseT }{@code >}}
     * 
     */
    @XmlElementDecl(namespace = "", name = "expenseReport")
    public JAXBElement<ExpenseT> createExpenseReport(ExpenseT value) {
        return new JAXBElement<ExpenseT>(_ExpenseReport_QNAME, ExpenseT.class, null, value);
    }

}

【问题讨论】:

标签: jakarta-ee java java-7


【解决方案1】:

当你的类路径引用JRE而不是项目所在的JDK时会出现这个问题,做一件事去

构建路径 > 库

删除包含的 Runtime 并添加 jdk runtime ,它现在应该可以解决您的问题了。

【讨论】:

  • 我在使用 maven-jaxb2-plugin 和 Maven/JAXB/m2e/Eclipse 时遇到了这个问题。 Eclipse 使用的是 Java 1.5,所以我添加了 maven-compiler-plugin 以将我的源和目标版本设置为 1.7,一切都很好。
  • 我认为@NathanielWaisbrot 是正确的(至少在某些情况下)。我有同样的问题,但是使用JDK。为 Java 1.7 而不是 1.5 设置 maven 插件也为我解决了问题。
  • 在 java 8 中,我在实例化 com.sun.security.auth.module.NTSystem NTSystem = new com.sun.security.auth.module.NTSystem(); 时遇到了同样的错误将 java 合规性更改为 1.5 为我解决了这个问题。
【解决方案2】:

我收到下一个错误:访问限制:

由于所需库 C:\Program Files (x86)\Java\jre7\lib\rt.jar 的限制,无法访问类型 XmlRegistry

这是我解决相同问题的方法:

1.Go to the Build Path settings in the project properties. (Right click on your application, then in the popout window you will see 'Build Path', slide right and click on 'Configure Build Path...')
2.Remove the JRE System Library
3.Add it back; Select "Add Library" and select the JRE System Library. 

默认设置对我有用。

【讨论】:

    【解决方案3】:

    只需删除并重新添加相同的 JRE 系统库。

    注意:确保在删除和重新添加时单击“确定”并关闭对话框。

    【讨论】:

      【解决方案4】:

      这种情况可以这样解决:

      1 - 转到项目属性对话框的 Java 编译器部分,然后转到错误和警告。 2 - 选中启用项目特定设置。 3 - 在已弃用和受限的 API 中,将 Forbidden 引用(访问规则)更改为 Warning 或 Ignore。

      4 - 请不要在你的祈祷中忘记我

      【讨论】:

        猜你喜欢
        • 2012-03-05
        • 2016-08-06
        • 2011-05-10
        • 2012-02-21
        • 2010-10-26
        相关资源
        最近更新 更多