Java如何在运行时将lib / rt.jar链接到您的应用程序?(How does Java link lib/rt.jar to your app at runtime?)

编程入门 行业动态 更新时间:2024-10-10 16:18:52
Java如何在运行时将lib / rt.jar链接到您的应用程序?(How does Java link lib/rt.jar to your app at runtime?)

Java标准/系统库( java.* , javax.*等)存储在每个JRE分发中的lib/rt.jar 。

假设我有一个已编译的应用程序,并将其变为myapp.jar 。 此JAR仅包含我的应用程序的类文件,仅引用系统类,如System , File , Runtime , Thread , String , Boolean等。

因此,当我运行我的应用程序时,通过java -jar myapp.jar说,JVM显然是在最后一刻链接(或其他)执行我的类文件的字节码(在myapp.jar )然后“跳转”进入lib/rt.jar运行位于那里的字节码。 我想如果myapp.jar依赖于运行时classpath上提供的其他JAR,则该过程是相同的。

我的问题是:这个“链接”过程叫什么,它本质上是如何工作的?

The Java standard/system libraries (java.*, javax.*, etc.) are stored in lib/rt.jar inside each JRE distribution.

Say I have an application that I have compiled and jarred into myapp.jar. This JAR only contains my app's class files, and merely references system classes like System, File, Runtime, Thread, String, Boolean, etc.

So when I run my app, say via java -jar myapp.jar, the JVM is obviously doing so last minute linking (or something) where it is executing the bytecode of my class files (inside myapp.jar) and then "jumping" into lib/rt.jar to run bytecode located there. I would imagine the process is the same if myapp.jar depends on other JARs provided on the runtime classpath.

My question is: what is this "linking" process called, and how does it essentially work?

最满意答案

rt.jar是bootstrap类路径的一部分,它是您已经知道的常用类路径的父级,并且您在使用-cp选项时进行配置(您实际上也可以使用-Xbootclasspath选项来加载引导类路径,例如,自定义Java运行时)。

有关如何从系统定义的类路径层次结构中搜索/加载类的详细说明 ,请参阅Oracle文档。

现在,您似乎还有其他问题:

如何找到存档?

它只是硬编码。 如果java二进制文件位于<common_root>/bin/java ,则将在<common_root>/lib/rt.jar搜索rt.jar。

如何进行“链接”?

在JVM上没有实际链接,使用基于ClassLoader层次结构的机制动态加载类,ClassLoader是实际执行类文件加载/解析的软件组件。 当您尝试加载类时,搜索从面向应用程序的默认类加载器(或者如果已定义了一个子类加载器)开始,如果无法加载该类,则使用父类加载器重复加载尝试,直到引导类加载器为到达。

如果找到该类,则会加载,解析.class文件,并创建表示类及其数据的内部结构。一旦加载了类,就可以创建一个新实例。 相反,即使引导类加载器无法加载您的类,也会抛出用户可见的ClassNotFoundException 。

That rt.jar is part of the bootstrap classpath, a parent of the usual classpath you already know and that you configure when you use the -cp option (you can actually change the bootstrap classpath too using the -Xbootclasspath option to load, for example, a custom Java runtime).

See Oracle documentation for a detailed description of how classes are searched/loaded from the system defined classpaths hierarchy.

Now, the additional questions you seemed to have:

How is the archive actually found?

It's simply hardcoded. If the java binary is located in <common_root>/bin/java, rt.jar will be searched in <common_root>/lib/rt.jar.

How is the "linking" performed?

On the JVM there is no actual linking, the classes are dynamically loaded using a mechanism based on a hierarchy of ClassLoader that are the software components that actually do the class file loading/parsing. When you try to load a class, the search starts from the application-facing default classloader(or a child classloader if you have defined one) and if the class cannot be loaded the loading attempt is repeated with a parent classloader until the bootstrap classloader is reached.

If the class is found, the .class file is loaded, parsed and internal structures representing the class and its data are created.Once the class is loaded a new instance can be created. If instead, even the boot classloader could not load your class a user-visible ClassNotFoundException is thrown.

更多推荐

本文发布于:2023-07-27 08:33:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1287780.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:您的   时将   应用程序   链接   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!