How does rt.jar works?

转载自:https://stackoverflow.com/questions/30222702/how-does-java-link-lib-rt-jar-to-your-app-at-runtime

 

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:

  1. 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.

  2. 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.

 

1)的rt.jar代表运行,并包含所有的核心Java运行时环境编译的类文件。

2)您必须在类路径中包含rt.jar,否则您无法访问核心类,例如java.lang.String,java.lang.Thread,java.util.ArrayList或java.io.InputStream以及Java API中的所有其他类。您可以通过使用WinRAR或WinZip客户端打开它,实际上可以看到rt.jar中的内容。您可以看到它不仅包含所有Java API,而且还包含com包中指定的内部类。

3)在windows中,rt.jar将始终位于$ JAVA_HOME/jre/lib下,其中$ JAVA_HOME指向JDK安装目录。即使你不安装JDK并只安装JRE,你也会看到它在完全相同的位置,你不会在$ JAVA_HOME/lib目录中找到rt.jar。顺便说一下,在MacOSX上,它被称为classes.jar,位于/ System/Library/Frameworks // Classes目录下。在以下截图中,您可以看到rt.jar位于Windows 8中的JRE lib目录中。

4)rt.jar是所有Java程序包驻留的位置。例如,如果一个类文件需要引用java.util.concurrent包中的一个类,例如ConcurrentHashMap,则JVM将在rt.jar中查找它,从而使其能够正确运行。

5)Java程序员问的另外一个问题是,我在哪里可以找到包含在rt.jar中的类的源代码?好吧,如果你已经安装了JDK,而不是JRE,那么你可以在$ JAVA_HOME/src.zip文件中找到所有的源代码。顺便说一句,sun。*源也包含在src.zip中,但这是专有的闭源Oracle代码。我还建议你将这个JAR文件包含在你的Eclipse中,这样你只需要输入Ctrl + T和类的名字就可以查看任何JDK类的源代码,其余部分将被Eclipse的Java类型搜索功能所关注。

6)了解rt.jar最重要的一件事就是JAR文件中的所有类都为JVM所知,这意味着JVM不会执行从加载其他JAR时执行的所有检查任何其他地点。这是由于各种性能原因而完成的,这就是为什么这些类由自举或原始类加载器加载的原因。不要试图将你的类文件包含在rt.jar中,因为它不提供Java的建议。它也与任何安全性妥协。

7)如果您对Java平台使用的不同二进制和JAR文件感兴趣,请查看此图。你可以看到JDK有三个主文件夹bin,lib和jre。 bin目录包含所有二进制可执java.exe运行Java程序,javac.exe编译Java程序等。lib包含tools.jar和dt.jar。 jre文件夹再次包含bin和lib目录。它位于这个lib目录中的rt.jar驻留。顺便说一下,这些文件和文件夹都做了什么,请查看Oracle官方网页。它们非常全面和描述性。

enter image description here

这是所有关于Java中的rt.jar文件。现在你知道rt.jar的目的是什么,为什么你不应该搞砸它。你可以在$ JAVA_HOME/jre/lib目录下找到这个JAR文件,我鼓励你自己去看看。

了解更多:http://javarevisited.blogspot.com/2015/01/what-is-rtjar-in-javajdkjre-why-its-important.html#ixzz3xkhLYtjP

 

posted @ 2019-04-15 14:40  井蛙不可语海  阅读(123)  评论(0编辑  收藏  举报