Java类路径加载问题
我想知道如果我有一个带有一对耳朵和战争的应用程序,并且在每个类中都名为“测试”,会发生什么? 这是罐子地狱吗? 哪些类会首先加载(从耳朵或从战争)? 请把我送到某个地方,在那里我可以更详细地了解这个棘手的案件。
I'm wonder what would happen, if I have an application with couple ears and wars, and in each of them class named "Test"?
Is it jar hell?
What classes would load first (from ears or from wars)?
Please, send me somewhere, where I can read about this tricky case more detailed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Servlet 容器通常使用每个 Web 应用一个类加载器的策略来避免这种情况。因此,如果您在同一个容器上部署了两个应用程序,则它们将具有不同的类加载器来满足它们的类解析和加载需求。 Tomcat 有一个相关文档。
当然,也有一些问题,比如使用 JDBC 驱动程序(每个 JVM 进程一个)和 JNI(给定的库不能被给定的 JVM 加载多次或类似的东西),但一般工作仍然是相同的。
Servlet containers normally use a one-classloader-per-webapp strategy to avoid this hell. So if you have two applications deployed on the same container, the will have different classloaders which satisfy their class resolution and loading needs. Tomcat has a document for this.
Of course, there are some gotchas like working with JDBC drivers (which are one per JVM process) and JNI (a given library can't be loaded more than once by a given JVM or something along those lines) but the general working remains the same.
你必须使用包。
com.foo.Test
与com.foo2.Test
不同。You have to use packages.
com.foo.Test
is different fromcom.foo2.Test
.