Detailed explanation of rt.jar in java

What is rt. jar?

rt.jar stands for runtime JAR and contains bootstrap classes - all classes from the Core Java API. Many Java developers don't know what is rt.jar? Often confused about the role of the rt.jar file, or wondering why the rt.jar file is used in Java?

In Windows and Linux, rt.jar is located in the lib directory of JRE. Before JDK 1.7, it was called classes.jar in MacOSX, and it was also changed to rt.jar since Java 7. Many developers try to put their own classes into rt.jar to solve the problems related to the class path, but it is best not to do this, because rt.jar contains the class files trusted by the JVM, and the JVM will not load other class files. The files are subject to strict security checks.

In this article, we will discuss the related content of rt.jar. For those who are new to Java and are not familiar with the development of JAR files, rt.jar is a zip-like compressed file, precisely called "Java archive" (Jar), ​​which stores all the resources required by Java class files and programs. It can also contain mainfest files, and it can also contain Main-Class entries, turned into executable JARs, run using the java -jar command.

  • rt.jar stands for runtime and contains all compiled calss files of the core Java runtime environment.
  • You must include rt.jar in your classpath, otherwise you don't have access to core classes such as java.lang.String, java.lang.Thread, java.util.ArrayList or java.io.InputStream, etc., and in all Java APIs other kind. You can use the IDE to open and view the content in rt.jar, which not only contains all Java APIs, but also contains the internal classes specified in the com package.

 

  • In windows, rt.jar is located under $JAVA_HOME/jre/lib. Even without the JDK installed and only the JRE, you see it in the exact same location, you can't find rt.jar in the $JAVA_HOME/lib directory. Incidentally, on MacOSX it's called classes.jarand and is located in the /System/Library/Frameworks//Classes directory. In the screenshot below, you can see that rt.jar is located in the lib directory of the JRE in Windows.
  • rt.jar is where all the Java packages are located. For example, you need to reference a class from java.util.concurrentpackage, such as ConcurrentHashMap, and the JVM will look it up from rt.jar, so that the program can run correctly.
  • Another question frequently asked by Java developers, where can I find the source code for the classes contained in rt.jar? Those who have installed JDK can find all source code in $JAVA_HOME/src.zip file. BTW, the sun.* sources are also included in src.zip, but this is proprietary closed-source Oracle code. It is recommended to add this JAR file in Eclipse, just type Ctrl+T followed by the class name to view the source code of any JDK class, and the rest will be handled by Eclipse's Java type search feature.
  • One very important thing about rt.jar is that the JVM is aware of all the classes in this JAR file, which means that the JVM does not perform checks when loading it from any location. This is done for various performance reasons, which is why these classes are loaded by bootstrap or primodial classloaders. So it is best not to try to include your own class files in rt.jar, and Java officials do not recommend doing so.
  • If you are curious about the different binaries and JAR files used by the Java platform, see the image below, JDK has three main folders bin, lib and jre. The bin directory contains all binary files, java.exe can be executed to run Java programs, and javac.exe compiles Java programs. The lib directory contains tools.jar and dt.jar. The jre folder also contains the bin and lib directories. rt.jar is in the lib directory here. BTW, if you want to fully understand the functions of each file and folder, please check Oracle's official introduction, which is very comprehensive and specific.

Guess you like

Origin blog.csdn.net/young_man2/article/details/126718729