java 加载jar包顺序,从lib目录加载jar文件的顺序

Could anyone explain the order in which jar files are loaded from the lib directory within Tomcat?

Is it alphabetical? Random? Or some other order?

解决方案

It's all described in Tomcat's ClassLoading HOW-TO. It's not necessarily in alphabetic order. If you observed that behaviour, it should absolutely not be relied upon if you intend to keep your webapp portable across servers. For example, Tomcat 6 "coincidentally" orders it, but Tomcat 8 does not.

Summarized, the loading order is as follows:

bootstrap/system (JRE/lib, then server.loader)

webapp libraries (WEB-INF/classes, then WEB-INF/lib)

common libraries (common.loader, then Tomcat/lib)

webapp-shared libraries (shared.loader)

If you would like to guarantee that JAR X is loaded after JAR Y, then you'd need to put JAR X in one of the places which appears later in the listing above.

There are exceptions however, which are mentioned in the tomcat docs

Lastly, the web application class loader will always delegate first for JavaEE API classes for the specifications implemented by Tomcat (Servlet, JSP, EL, WebSocket). All other class loaders in Tomcat follow the usual delegation pattern.

That means if a webapp contains any JavaEE classes (javax.*), then it will be ignored by tomcat.

For each loader, the classes are just loaded by the JVM in the order whenever they needs to be imported/executed and are not loaded yet.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值