如果在两个jar文件中打包相同的类,将调用哪个java类文件? [英] Which java class file will be called if same class is packed in two jar files?

查看:1246
本文介绍了如果在两个jar文件中打包相同的类,将调用哪个java类文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发开源项目。截至目前,我在任何课程中都没有任何自定义。所以使用opensource项目提供的所有jar文件。我的问题是,如果我修改一个java文件,编译它并将其打包为具有相同文件夹结构的新jar文件,启动服务器或运行时是否会出现任何问题?如果没有调用哪个类文件(默认文件或我的自定义java类文件)?

解决方案

实际上它取决于很多因素:




  • 如果两个jar文件都在同一个ClassLoader中,例如Java类路径( - cp 选项),通常它应该是jar列表顺序中找到的第一个文件。


  • 如果部署在JavaEE容器中,比如在EAR文件或 WEB-INF / lib 或WAR文件中,不保证容器将在两个初创公司之间加载相同的类。在这种情况下,唯一确定的是在$ code> WEB-INF / lib 之前搜索 WEB-INF / classes / p>


  • 在复杂的ClassLoader层次结构中,默认行为是父优先搜索,但JavaEE实现引入了父内容策略(WebSphere)或过滤等机制,这要归功于部署描述符(WebLogic)




一个选项可能是声明 META-INF / MANIFEST.MF中的#main%20Attributes> jar文件依赖项 文件归功于 Class-Path 属性。它应该在ClassLoader级别强制执行加载顺序,特别是在使用 java -jar myapp.jar 启动时,但它可能依赖于JavaEE上下文中的实现。



备注:使用OpenSource项目时,提交变更请求并发布您的更改或改进以便社区从中受益可能是公平的。然后您的项目可能会更新到主流,而不会在ClassPath中出现如此困难的野生补丁。


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.

这篇关于如果在两个jar文件中打包相同的类,将调用哪个java类文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆