Java / JDK / JRE中的rt.jar是什么? 为什么重要?

最近发现,在idea中查看util包的源码时,发现几乎所有的Core APIs,都位于rt.jar这个路径下,例如util包,lang包等,****上几乎都找不到rt.jar相关的介绍,因此google了下,找到了一篇相关的文章,以此记录,分享一下,翻译有调整,末尾附英文原版。

Java / JDK / JRE中的rt.jar是什么? 为什么重要?

rt.jar代表runtime JAR,并且包含引导类(bootstrap classes)——来自Core Java API的所有类。很多Java开发都不知道什么是rt.jar?经常混淆rt.jar文件的作用,或疑惑在Java中为什么使用rt.jar文件?

Windows和Linux中,rt.jar位于JRE的lib目录下。JDK 1.7之前,MacOSX中叫classes.jar,从Java 7开始也改为rt.jar。很多开发尝试把自己的类放进rt.jar中,来解决与类路径相关的问题,但最好不要这样做,因为rt.jar包含了JVM信任的class文件,JVM加载时不会对其他class文件进行严格的安全检查。

在本文中,我们将讨论与rt.jar相关的内容。对刚接触Java而且不熟悉JAR文件的开发来说,rt.jar是一个类似zip的压缩文件,精确称为“Java archive”(Jar),存储了Java class文件和程序所需的全部资源。它还可以包含mainfest文件,还可以包含Main-Class条目,变成可执行JAR,使用java -jar命令来运行。

  1. rt.jar代表runtime,包含所有核心Java 运行环境的已编译calss文件。
  2. 必须在类路径中包含rt.jar,否则您无权访问核心类,例如 java.lang.String,java.lang.Thread,java.util.ArrayList或java.io.InputStream等,以及所有Java API中的其他类。 可以使用IDE打开并查看rt.jar中的内容, 它不仅包含所有Java API,还包含com包中指定的内部类。
    Java / JDK / JRE中的rt.jar是什么? 为什么重要?
  3. 在windows中,rt.jar位于$ JAVA_HOME / jre / lib下。 即使没有安装JDK并且只安装JRE,也会在完全相同的位置看到它,在$ JAVA_HOME / lib目录中找不到rt.jar。 顺便提一句,在MacOSX上,它被称为classes.jarand,位于/ System / Library / Frameworks // Classes目录下。 下面的截图中,可以看到rt.jar位于Windows 中JRE的lib目录中。
    Java / JDK / JRE中的rt.jar是什么? 为什么重要?
  4. rt.jar是所有Java包所在的位置。例如,需要从java.util.concurrentpackage引用类,比如 ConcurrentHashMap,JVM会从rt.jar中查找它,从而使程序能够正确运行。
  5. 另一个Java开发常问的问题,在哪里可以找到rt.jar中包含的类的源码呢?安装了JDK的可以在$ JAVA_HOME / src.zip文件中找到所有源码。BTW,sun.* 源码也包含在src.zip中,但这是专有的闭源Oracle代码。 建议在Eclipse中添加这个JAR文件,只需键入Ctrl + T和类名就可以查看任何JDK类的源码,其余部分将由Eclipse的Java类型搜索功能处理。
  6. 关于rt.jar一个很重要的事就是,JVM知道这个JAR文件中的所有类,这意味着JVM在从任何位置加载时都不会执行检查。 之所以这样做是由于各种性能原因,这就是为什么这些class由bootstrap或primodial类加载器加载的原因。 所以最好不要尝试将自己类文件包含在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目录。 rt.jar就在这里的lib目录中。 BTW,如果想全面理解每个文件和文件夹的功能,请查看Oracle官方介绍,非常全面和具体。

以上就是关于rt.jar文件的全部内容。 现在知道rt.jar的目的是什么,以及为什么你不应该惹它了吧。 你还可以在$ JAVA_HOME / jre / lib目录中找到JAR文件,建议亲自看下。
————————————————————————————————————
以下是原文:

What is rt.jar in Java/JDK/JRE? Why it’s Important?

rt.jar stands for runtime JAR and contains the bootstrap classes, I mean all the classes from Core Java API. I have found that many Java programmer doesn’t know what is rt.jar? and often confused with the role of rt.jar file or why we use of rt.jar file in Java?

No surprise, the name is little bit cryptic. This file always reside inside lib directory of JRE, at least in Windows and Linux. In MacOSX it reside at different location and also has different name i.e. classes.jar, but that is only prior to JDK 1.7. From Java 7 release Apple has stopped distributing Java and if you separately install, it will have same name as rt.jar. Many developer thinks to include their classes inside rt.jar to solve classpath related problems, but that is a bad idea. You should never be messing with rt.jar, it contains class files which is trusted by JVM and loaded without stringent security check it does for other class files.

In this article, we will learn some interesting things about this magical JAR from Java world. For those programmers, who are new to Java and not familiar with JAR file, it is a zip like file, precisely known as Java archive which stores Java class files and any resource needed by program. It can also contain mainfest file, which can include Main-Class entry to make it an executable JAR, which can be run by using java -jar command.

  1. rt.jar stands for runtime and contains all of the compiled class files for the core Java Runtime environment.

  2. You must include rt.jar in your classpath, otherwise you don’t have access to core classes e.g. java.lang.String, java.lang.Thread, java.util.ArrayList or java.io.InputStream and all other classes from Java API. You can actually see what is inside rt.jar by opening it by using WinRAR or WinZip client. You can see that it not only contains all Java API but also internal classes specified in com package.

  3. In windows, rt.jar will always reside under $JAVA_HOME/jre/lib, where $JAVA_HOME refers to JDK installation directory. Even if you don’t install JDK and just install JRE, you will see it in exactly same location, you won’t find rt.jar inside $JAVA_HOME/lib directory. BTW, On MacOSX it is called classes.jarand located under /System/Library/Frameworks//Classes directory. In following screenshot you can see that rt.jar is inside JRE’s lib directory in Windows 8.

  4. The rt.jar is where all the Java packages reside. For example, if a class file need to refer a class from java.util.concurrentpackage e.g. ConcurrentHashMap, then the JVM will look for it inside the rt.jar, thus enabling it to run correctly.

  5. One more question Java programmer ask is, where can I find source code for classes included in rt.jar? well, if you have installed JDK, not JRE then you can find all sources inside $JAVA_HOME/src.zip file. BTW, sun.* sources are also included in src.zip but that is proprietary closed source Oracle code. I also suggest you to include this JAR file in your Eclipse, so that you can view source code of any JDK class by just typing Ctrl + T and name of the class, rest will be taken care by Eclipse’s Java type search functionality.

  6. One of the most important thing to know about rt.jar is that all the classes in this JAR file is known to JVM, which means JVM doesn’t do all the checks it does while loading any other JAR from any other location. This is done due to various performance reason and that’s why these classes are loaded by bootstrap or primodial class loaders. Don’t try to include your class files in rt.jar, as its not advised by Java. It also compromise with any security.

  7. If you are curious about different binary and JAR files used by Java platform, then look into this diagram. You can see that JDK has three main folders bin, lib and jre. bin directory contains all binary executable e.g. java.exe to run Java program, javac.exe to compile Java program etc. lib contains tools.jar and dt.jar. jre folder again contain bin and lib directory. It’s in this lib directory rt.jar reside. By the way for complete explanation of what each of these file and folder does, checkout Oracle official pages. They are very comprehensive and descriptive.

That’s all about rt.jar file in Java. Now you know what is the purpose of rt.jar and why you should not mess with it. You can find this JAR file inside $JAVA_HOME/jre/lib directory and I encourage you to look it by yourself.