Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Tuesday, July 27, 2021

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 a little bit cryptic.  This file always resides inside the lib directory of JRE, at least in Windows and Linux. In MacOSX it resides at a different location and also has a 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 the same name as rt.jar.

Many developers think 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 the Java world. For those programmers, who are new to Java and not familiar with the JAR file, it is a zip like a file, precisely known as the Java archive which stores Java class files and any resource needed by the program. It can also contain a manifest file, which can include Main-Class entry to make it an executable JAR, which can be run by using java -jar command.

Important Points about rt.jar in Java

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

What is rt.jar in Java

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

In the following screenshot, you can see that rt.jar is inside JRE's lib directory in Windows 8. 

Where to find rt.jar in Windows, Linux and MacOSX

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.concurrent package 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/ file. BTW, sun.* sources are also included in 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 the source code of any JDK class by just typing Ctrl + T and the name of the class, the rest will be taken care of by Eclipse's Java type search functionality.

6) One of the most important things 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 reasons and that's why these classes are loaded by bootstrap or primordial class loaders. Don't try to include your class files in rt.jar, as its not advised by Java. It also compromises with any security.

7) If you are curious about different binary and JAR files used by the Java platform, then look into this diagram. You can see that JDK has three main folders bin, lib, and jre. the 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 contains bin and lib directory. It's in this lib directory rt.jar reside.

By the way for a complete explanation of what each of these files and folders does, check out Oracle's official pages. They are very comprehensive and descriptive.

basic folders and files in JDK JRE

That's all about the 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 the $JAVA_HOME/jre/lib directory and I encourage you to look it by yourself.


Arzu BT said...

I'm not a norm but as I follow, we gonna say goodbye to rt.jar in this year with Java 9. I think, every java user should be aware of rt.jar :).

Anonymous said...

@Arzu, what is going to replace rt.jar? If I understood correctly all Java biaries are in this JAR so there must be something? are they simply renaming rt.jar in Java 9?

Java basic concept said...


Anonymous said...

Good articles

Post a Comment