解决java中的类名冲突

发布于 2024-09-25 01:15:01 字数 411 浏览 6 评论 0原文

我遇到一种情况,我必须加载一个命名类。如果我的类路径中有多个具有相同名称的类(例如,com.example.myclass),我必须加载这两个类。我正在使用 CustomLoader 类的“loadClass()”方法,该方法派生自 java.lang.ClassLoader。我没有改变父类的行为,只是简单地调用父类的方法。我的问题是,如果有两个同名的类,我只能加载其中之一。我已经在网上扫描了解决方案,但没有找到任何解决方案。我发现了许多通过创建新的类加载器实例来重新加载类的解决方案,但就我而言,新实例可能最终会再次加载第一个类。这个问题能解决吗?

编辑:我忘了提及两个同名的类位于不同的 jar 文件中。

编辑:乔恩和斯蒂芬都给出了相同的解决方案,但我只能将一个标记为答案。抱歉:(。不过,我对两个答案都投了赞成票。

I have a situation where I have to load a named class. If there are multiple classes with the same name (say, com.example.myclass) in my class path I have to load both. I am using 'loadClass()' method of my CustomLoader class which derives from java.lang.ClassLoader. I have not changed the behaviour of the parent class but am simply calling the parent's methods. My problem is that if there are two classes with the same name, I am able to load only one of them. I have scanned the web for a solution but haven't found any. I found many solutions on reloading classes by creating a new class loader instance but in my case the new instance will probably end up loading the first class again. Can this problem be solved?

EDIT: I forgot to mention that the two classes with the same name are in different jar files.

EDIT: Both Jon and Stephen gave the same solution but I can mark only one as answer. Sorry :(. I have up voted both answers though.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

兔小萌 2024-10-02 01:15:01

我相信 JVM 和类库假设类名在类加载器中是唯一的。因此,如果您想为不同的类加载相同的类名,您将需要不同的类加载器(例如每个 jar 文件一个)。

使用它们可能很棘手,但这就是此类事情遇到的问题。

I believe the JVM and class libraries assume that a class name is unique within a classloader. Therefore if you want to load the same class name for different classes, you'll need different classloaders (e.g. one for each jar file).

Using them could be tricky, but that's the sort of problem you get with this sort of thing.

坐在坟头思考人生 2024-10-02 01:15:01

类加载器 API 无法指定应用程序尝试加载两个同名类中的哪一个。

您将需要使用不同的类路径配置两个不同的类加载器。然后,您需要通过在相应的类加载器上调用 loadClass(String) 方法来加载类。

做这种事情有很多“陷阱”。其中之一是各个类的实例的类型会不同,并且从一种类型到另一种类型的类型转换将会失败。

The classloader API has no way to specify which of the two classes with the same name that your application is trying to load.

You will need to configure two different classloaders with different classpaths. Then you will need to load the classes by calling the loadClass(String) method on the respective classloaders.

There are a number of "gotchas" with doing this kind of thing. One of them is that the types of the instances of the respective classes will be different, and typecasts from one type to the other will fail.

情话墙 2024-10-02 01:15:01

我认为这里的问题存在严重问题。这种情况怎么可能发生呢?开发过程中存在严重错误,可能会首先产生重复的类。

或者这就是这试图解决的问题?或者诊断?

I suggest that there is something seriously wrong with the problem here. How can such a state of affairs possibly come about in the first place? There is something seriously wrong with a development process that can produce duplicate classes in the first place.

Or is that the problem that this is trying to solve? Or diagnose?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文