java - 将 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。这很重要,我忘了提。
解决方案
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(...)
推荐阅读
- c++ - 与朋友类不同的两个类声明是否违反了 ODR?
- html - 如何解决图像布局/定位问题?
- amazon-web-services - Upload data to Redshift with PySpark
- python - 如何使用 Python 在多个实例中将 CSV 中的重复项添加到特定行的末尾?
- sql - 在 postgresql 中合并两个具有公共列名的 SQL 查询
- python - 我在 subprocess.run 上发现错误了吗?
- azure - 在 Service Fabric 中运行 Azure DevOps 自托管生成代理时出现“无法解析远程名称”
- java - 如何显示链表
在爪哇 - javascript - 使用 CSS 和 JS 创建下拉菜单
- javascript - 为什么这些图标中的一个有效,而另一个无效?