java 封装类的引用,如果在两个jar文件中打包相同的类,将调用哪个java类文件?...

I am working on opensource project. As of of now I don't have any customization in any class. So using all the jars files provided by opensource project. My question is if I modify one java file, compile it and pack it new jar file with same folder structure, will there be any issue at start up of server or run time? If not which class file will be called(default file or my customize java class file)?

解决方案

In fact it depends on many factors:

If both jar files are in the same ClassLoader, for instance the Java classpath (-cp option), normally it should be the first file found in the jar list order.

If deployed in a JavaEE container, like in an EAR file or in WEB-INF/lib or a WAR file, there is no warranty the container will load the same class between two startups. In that context, the only thing sure is that WEB-INF/classes is searched before WEB-INF/lib

In a complex ClassLoader hierarchy, the default behavior is parent-first search but JavaEE implementations have introduced mechanisms like parent-last policy (WebSphere) or filtering thanks to deployment descriptors (WebLogic)

An option may be to declare jar file dependencies in META-INF/MANIFEST.MF file thanks to the Class-Path attribute. It should enforce a loading order at the ClassLoader level, particularly when started with java -jar myapp.jar but it may depend on implementations in a JavaEE context.

Remark: when using an OpenSource project, it may be fair to submit a change request and publish your changes or improvements so that the community takes benefits from it. Then your project may update to main stream without such a difficulty of wild patches in your ClassPath.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值