在不同的JAR中加载两个类 [英] Loading two classes in different JARs

查看:202
本文介绍了在不同的JAR中加载两个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的JAR中使用相同的包获得了两个类。在之前的版本中,两个类都是相同的,所以我在加载它们时没有任何问题。现在,其中一个添加了一个新方法,如果我想访问它,不仅应该使用该包导入类,还需要确保具有正确类的jar首先出现在类路径中。

I got two classes with the same package in different JARs. Until the previous version, both classes were identical, so i had no issues in loading them. Now, one of them has a new method added and if I want to access it, not only should I import the class with that package, i also need to make sure the jar with the correct class comes first in the classpath.

ie javac -classpath%classpath%; a.jar; b.jarMyClasses ..

其中 a.jar 使用我的新方法使用该类。

where a.jar has the class with my new method.

现在,当我的应用程序投入生产时如何确保这一点,它被部署为EAR文件,所有库都在WEB-INF / lib下?

Now, how do i ensure this when my app goes to production, where it's deployed as an EAR file, with all the libraries under WEB-INF/lib?

我如何知道哪个jar优先于另一个?是a.jar的字母顺序是否优先于b.jar?

How do I know which jar gets the preference over the other? Is it the alphabetical order like a.jar is given the first preference over b.jar?

我读过这个 safe-class-imports-from-jar-files 帖子,并且开始了解编写自定义类加载器,但有没有更好的更简单的解决方案?我只是在这个当前项目的整个JAR中访问这个方法并编写一个类加载器似乎有点矫枉过正。

I've read this safe-class-imports-from-jar-files thread and got to know about writing a custom classloader, but is there a better simpler solution that? Cos I'm just going to access this method in that whole JAR in this current project and writing a classloader seems a bit overkill.

请不要问我为什么在不同的JAR中同一个包具有相同的类?它绝对不受我的控制并且需要一些时间来纠正这个问题。

And please don't ask me "Why the hell same class with same package in different JARs?" It's absolutely out of my control and it'll take some time to get this corrected.

环境细节:他们的1.5 Java上的IBM WAS 6.1 。

Environment details: IBM WAS 6.1 on their 1.5 Java.

如果我没有多大意义,请问我更多问题。在此先感谢!

Please ask me more questions, if I don't make much sense. Thanks in advance!

推荐答案

Websphere允许您指定在搜索课程时查询特定应用程序的类加载器的顺序(类加载器是分层结构的,从最顶层加载JRE类,再加载到WAR中的类加载器类)。

Websphere allows you to specify the order in which classloaders of a particular application are inquired when searching for a class (the classloaders are hierarchically structured, from the topmost that loads JRE classes, down to classloader loading classes in your WAR).

在部署应用程序期间,您可以指定在搜索类时是否查询类加载器的顺序。有两种模式 - Parent first(即首先查询最顶层的类加载器)和parent last(首先查询app classloader)。这可以在EAR和WAR级别指定。

During deployment of an app, you can specify if the order of inquiring the classloaders when searching for a class. There are two modes - Parent first (i.e. query the topmost classloader first) and parent last (query the app classloader first). This can be specified on both EAR and WAR level.

将复制的jar包装到应用程序中的不同位置(例如,一个到EAR的类路径,另一个到WAR的WEB-INF) / lib)并适当地设置classloader ordering可以解决您的问题。但是,如果两个JAR必须位于同一级别(例如WEB-INF / lib),则无法指定在加载重复的类时将使用哪一个。

Packaging the duplicated jars to different locations in the app (e.g. one to EAR's classpath, the other to WAR's WEB-INF/lib) and setting the classloader orderING apropriately may solve your problem. However, if both your JARs have to be on the same level (e.g. WEB-INF/lib), then there's no way to specify which one will be used when loading the duplicated class.

这篇关于在不同的JAR中加载两个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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