尝试理解 Java 类加载

发布于 2024-08-29 03:52:30 字数 755 浏览 11 评论 0原文

我目前正在了解 Java 和 OSGi,所以我读了几本书。在一本特定的书中描述了类加载。

您可以从作者页面(Neil Bartlett)下载它(免费且合法): OSGi Book

第 9 页和第 10 页是以下图片:

替代文本 http://img265.imageshack.us/img265/4127/picture1qct.png替代文字 http://img297.imageshack.us/img297/594/picture2yv.png

看起来好像在那里我们的类“Foo”可能不会使用 foobar.jar 的类“Bar”,而是使用 naughty.jar 中的类“Bar”。

由于 Java 类路径的扁平和全局结构,这可能是这样,但据我所知,您会从要导入某个类的位置定义一个包:

import foobar.Bar

这应该可以防止加载错误的类,不是吗?当然假设该包名为“foobar”。

I'm currently getting to know Java and OSGi, so I've read a few books. In one particular book the class loading is described.

You can download it (free and legal) from the authors page (Neil Bartlett):
OSGi Book

On page 9 and 10 are this pictures:

alt text http://img265.imageshack.us/img265/4127/picture1qct.pngalt text http://img297.imageshack.us/img297/594/picture2yv.png

It seems like there is the possibility that our class "Foo" won't use the class "Bar" of foobar.jar, but instead class "Bar" from naughty.jar.

Because of the flat and global structure of the Java classpath this could be, but as far as I know you would define a package from where you want to import a certain class:

import foobar.Bar

This should prevent loading the wrong class, shouldn't it? Of course assuming that the package is called "foobar".

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

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

发布评论

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

评论(5

暖风昔人 2024-09-05 03:52:31

import 语句与类加载无关 - 它只是允许您使用短名称 Bar 而不是完全限定的 foobar.Bar。如果 foobar.jarnaughty.jar 都包含具有完全限定名称 foobar.Bar 的类,则类加载器将从第一个加载该类类路径上包含所需类的 jar 文件。

The import statement has nothing to do with classloading - it just allows you to use short name Bar instead of the fully qualified foobar.Bar. If both foobar.jar and naughty.jar contain class with fully qualified name foobar.Bar, classloader will load the class from the from the first jar file with required class on the classpath.

故人如初 2024-09-05 03:52:31

好主意,但不幸的是包与 jar 文件名无关。您可以将任意包中的内容全部放在同一个 jar 文件中,并且任意 jar 文件名与不相关的包名称。由类加载器来为您解决它们。

good idea, but unfortunately packages are independent of the jar file names. you can have things in arbitrary packages all in the same jar file, and arbitrary jar file names with unrelated package names. it's up to the classloader to resolve them for you.

梦明 2024-09-05 03:52:31

问题是 foobar.jarnaughty.jar 可能都有一个类,其完全限定名称为 foobar.Bar。然后 foobar.Foo 解析 naughty.jarfoobar.Bar 而不是 foobar.Bar foob​​ar.jar

希望这有帮助。

The problem is both foobar.jar and naughty.jar might have a class that its fully qualified name is foobar.Bar. Then foobar.Foo resolves the foobar.Bar of naughty.jar instead of foobar.Bar of foobar.jar.

Hope this helps.

和我恋爱吧 2024-09-05 03:52:31

作者在这里假设 Bar 类的两个版本都位于同一个包中(否则就不会有任何问题)。如果 naughty.jar 是类路径中的“第一个”,则作者所描述的情况就会发生。在这种情况下,类加载器将选择顽皮的版本(类加载器按自然顺序扫描类路径并选择找到的第一个类)。

The author is assuming here that both versions of the Bar classes are in the same package (or there wouldn't be any problem). What the author is describing can happen if naughty.jar is "first" in the class path. In that case, the classloader would pick the naughty version (the classloader scans the classpath in the natural order and picks the first class found).

初见 2024-09-05 03:52:31

导入不允许您自由地从所需的 java.lang.Class 中加载类。您可以从这里阅读有关类加载器的更多信息 Java 类加载器

The import doesnt allow you the liberty of loading the class from the desired java. You can read more about classloaders from here Java Classloaders

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