尝试理解 Java 类加载
我目前正在了解 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
import
语句与类加载无关 - 它只是允许您使用短名称Bar
而不是完全限定的foobar.Bar
。如果foobar.jar
和naughty.jar
都包含具有完全限定名称foobar.Bar
的类,则类加载器将从第一个加载该类类路径上包含所需类的 jar 文件。The
import
statement has nothing to do with classloading - it just allows you to use short nameBar
instead of the fully qualifiedfoobar.Bar
. If bothfoobar.jar
andnaughty.jar
contain class with fully qualified namefoobar.Bar
, classloader will load the class from the from the first jar file with required class on the classpath.好主意,但不幸的是包与 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.
问题是
foobar.jar
和naughty.jar
可能都有一个类,其完全限定名称为foobar.Bar
。然后foobar.Foo
解析naughty.jar
的foobar.Bar
而不是的
。foobar.Bar
foobar.jar希望这有帮助。
The problem is both
foobar.jar
andnaughty.jar
might have a class that its fully qualified name isfoobar.Bar
. Thenfoobar.Foo
resolves thefoobar.Bar
ofnaughty.jar
instead offoobar.Bar
offoobar.jar
.Hope this helps.
作者在这里假设
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).导入不允许您自由地从所需的 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