类加载到接口

发布于 2024-09-18 15:32:30 字数 478 浏览 6 评论 0原文

我目前使用的平台(JDK 1.3、BD-J)受到很大限制。我想使用一个 JAR 文件尝试在加载时执行自我完整性检查,如果失败,它将进入不可操作状态。很难找出为什么会发生这种情况,但大多数消息来源都指出它无法通过 BD-J 结构找到/访问它自己,所以它死了。

这排除了在加载时使用它,而是在应用程序本身中加载它。这是一个相当大的库,所以我必须创建相当多的接口,以便我可以将加载的对象投射到它并可能使用它。这就是我的问题所在。

接口在正常加载时加载,然后在运行时加载库并转换为先前加载的接口,这是一个问题吗?我收到 ClassCastException

我已尽我所能将接口基于库公共方法,但是当我尝试转换为接口时,我收到 ClassCastException。注意:一切加载正常,我可以访问构造函数并读取方法名称。就在铸造它使其可用时,它失败了。

我的项目中的接口包与工具包中的接口包不同,这有关系吗?

我的想法已经用完了,有什么我忽略的吗?

谢谢。

I'm quite restricted in the platform I'm currently working on (JDK 1.3, BD-J). One JAR file I would like to use attempts to perform a self-integrity check on load and if it fails it goes into an inoperable state. It's quite difficult to find out why this is happening but most sources point to that it cannot find/access it self through the BD-J structure, so it dies.

This rules out using it at load time and instead to load it in the application itself. This is quite a large library so I have to create quite an amount of interfaces so I can cast a loaded object to it and potentially use it. This is where my problem lies.

The interfaces are loaded on normal load time and the library is then loaded during run time and casted to the previously loaded interfaces, is this a problem? I'm receiving ClassCastException

I've based the interfaces off the libraries public methods as best I can, but when I attempt to cast to an interface I receive the ClassCastException. Note: It all loads fine, I can access constructors and read the method names. Just when casting it for it to be useable it fails.

The interface packages are different in my project to that of the toolkit, does this matter?

I'm running out of ideas, is there something I have overlooked?

Thanks.

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

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

发布评论

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

评论(2

牵你手 2024-09-25 15:32:30

我不确定我是否完全理解您的问题是什么 - 也许有关类层次结构的更多详细信息将有助于弄清楚情况。从你写的内容我可以猜测两种可能的情况:

.1。您要使用的类未实现任何接口。

在这种情况下,无论您如何命名接口,都不会起作用,因为您正在加载的类没有实现它们。如果您无法将该 jar 作为启动类路径的一部分加载,那么您将不得不使用反射。

.2.您想要的类实现您尝试复制的某些接口。

在这种情况下,您的接口实现必须与类正在实现的接口的精确限定名称相匹配。通常,当从 jar 加载类时,类加载器将首先从系统类加载器中获取接口,从而加载您的接口,一切都应该正常工作。

但是,如果他们使用一些疯狂的内部类加载器,他们可能仍然会尝试加载自己的接口。您可以尝试使用“-XX:+TraceClassLoading”来弄清楚是否是这种情况,尽管我不知道 1.3 jre 是否会理解该选项。

现在,如果您愿意尝试更多,您也可以尝试另一种方法。编写您自己的类加载器,加载该 jar 中的类以及您想要运行的代码。这样,您的代码将能够直接引用该 jar 中的类,但要启动您的应用程序,“main”方法必须是初始化此类加载器、使用反射加载“真实”主类并执行的方法它的 main() 方法也是通过反射实现的。

I'm not sure I fully grok what your problem is - maybe some more details about what the class hierarchy looks like would help in figuring out the situation. From what you wrote I can guess two possible scenarios:

.1. The classes you want to use do not implement any interface.

In this case no matter what you name your interfaces, it will not work, since the classes you're loading do not implement them. You're stuck with using reflection if you can't load that jar as part of the boot classpath.

.2. The classes you want implement some interface that you're trying to replicate.

In this case you interface implementation must match the exact qualified name of the interface the classes are implementing. Normally, when loading the classes from the jar, the class loader will pick up the interfaces from the system class loader first, thus loading your interfaces, and everything should work.

If they use some crazy internal class loader, though, they might still try to load their own interfaces. You could try to figure out if that's the case by using "-XX:+TraceClassLoading", although I don't know if the 1.3 jre will understand that option.

Now if you're willing to experiment more, you could also try another approach. Write your own class loader that loads both the classes from that jar and the code you want to run. That way, your code would be able to directly refer to the classes in that jar, but to start your application the "main" method will have to be one that initializes this classloader, loads the "real" main class using reflection, and executes its main() method also via reflection.

半衾梦 2024-09-25 15:32:30

这些类很可能是由不同的类加载器加载的。 http://mindprod.com/jgloss/classloader.html 可能会给出一些想法。

Most probably the classes are loaded by different class loaders. http://mindprod.com/jgloss/classloader.html may give some idea.

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