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.

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

What is rt.jar in Java

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

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

basic folders and files in JDK JRE

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.


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?

Post a Comment