首页 > 解决方案 > 将 java 项目导出到 jar 更改了访问修饰符

问题描述

我有问题。最近我导出了不是我写的项目,所以我不能在这里拍它:/,到一个 JAR 文件。这是因为我想在我的项目中使用它。我遇到的问题是在 Eclipse 中更改了一个重要类中构造函数的访问修饰符。由于该导出项目的方法也不同,因此它是不可变的,无法访问它并引发 IllegalAccessError。有没有办法在导出时阻止这种变化?我做错了什么吗?

编辑:

public class ClassOne extends ClassOneParent {

    private final Logger logger;

    protected ClassOne(final Conf configuration,
                                  final Logger logger) {
        super(configuration);
        this.logger = logger;
    }

}

所以这是一个具有我需要访问的构造函数的类。

public abstract class ClassTwo extends ClassTwoParent {

    protected static ReturnedTypeINeed getReturnedType(final Logger logger) {
        final Conf configuration = new Conf();
        return new ClassOne(configuration, logger).getService();
    }
}

所以 ClassTwo 有一个方法试图在返回 ClassOne 构造函数中访问。通常,ClassOne 构造函数有一个公共修饰符并且一切正常,在导出到 jar 后它更改为受保护。有没有办法绕过它?ClassTwo 也是 jar 的一部分,所以我无法修改它。

编辑二:它是同一个 jar,但不同的包,这就是它抛出 IllegalAccessError 的原因,我从一个包含多个子项目的大项目中制作了一个 jar。这很重要,我忘了提。

标签: javaeclipsejaraccess-modifiers

解决方案


If you are trying to directly call ClassTwo.getReturnedType(logger) from outside the jar that won't work because it is protected and therefore not accessible. Otherwise I don't see anything wrong with your posted example code. If ClassTwo is in the same jar it should be able to call ClassOne(...)


推荐阅读