简体   繁体   English

试图了解Java类加载

[英]Trying to understand Java Classloading

I'm currently getting to know Java and OSGi, so I've read a few books. 我目前正在了解Java和OSGi,所以我读了几本书。 In one particular book the class loading is described. 在一本特定的书中,描述了类加载。

You can download it (free and legal) from the authors page (Neil Bartlett): OSGi Book 您可以从作者页面(Neil Bartlett)下载(免费和合法): OSGi书

On page 9 and 10 are this pictures: 这些图片在第9页和第10页:

alt text http://img265.imageshack.us/img265/4127/picture1qct.png alt text http://img297.imageshack.us/img297/594/picture2yv.png 替代文字http://img265.imageshack.us/img265/4127/picture1qct.png 替代文字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. 似乎我们的类“ Foo”可能不会使用foobar.jar的“ Bar”类,而是使用naughty.jar中的“ Bar”类。

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: 由于Java类路径的平面和全局结构,可能是这样,但是据我所知,您将从要导入某个类的位置定义一个包:

import foobar.Bar

This should prevent loading the wrong class, shouldn't it? 这应该避免加载错误的类,不是吗? Of course assuming that the package is called "foobar". 当然,假设该程序包被称为“ foobar”。

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

good idea, but unfortunately packages are independent of the jar file names. 好主意,但不幸的是,程序包与jar文件名无关。 you can have things in arbitrary packages all in the same jar file, and arbitrary jar file names with unrelated package names. 您可以将任意包中的内容全部放在同一个jar文件中,并将任意jar文件名包含不相关的包名。 it's up to the classloader to resolve them for you. 取决于类加载器为您解决它们。

The problem is both foobar.jar and naughty.jar might have a class that its fully qualified name is foobar.Bar . 问题在于foobar.jarnaughty.jar可能具有其完全限定名称为foobar.Bar Then foobar.Foo resolves the foobar.Bar of naughty.jar instead of foobar.Bar of foobar.jar . 然后foobar.Foo解析foobar.Barnaughty.jar而不是foobar.Barfoobar.jar

Hope this helps. 希望这可以帮助。

The author is assuming here that both versions of the Bar classes are in the same package (or there wouldn't be any problem). 作者在这里假设Bar类的两个版本都在同一包中(否则不会有任何问题)。 What the author is describing can happen if naughty.jar is "first" in the class path. 如果naughty.jar在类路径中是“ first”,则作者所描述的内容可能会发生。 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). 在这种情况下,类加载器将选择顽皮的版本(类加载器以自然顺序扫描类路径并选择找到的第一个类)。

The import doesnt allow you the liberty of loading the class from the desired java. 导入不允许您自由从所需的Java加载类。 You can read more about classloaders from here Java Classloaders 你可以阅读更多关于这里的类加载器的Java类加载器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM