Why do we use rt.jar in a java project?

What is the necessity of rt.jar ??


It contains all the classes provided in the Java Runtime Environment.

If you don't have it on your classpath you will not have access to any of those classes you need to use like java.lang.String or java.io.File.


rt = Run Time

It contains all the java runtime libraries. (Essential)


Cross compilation is one case where you have to explicitly use it.

E.g., if you are on Java 8, and want to compile Java 7 while rejecting Java 8 extensions. So you could try:

javac -source 1.7 Main.java

But then javac will say: warning: [options] bootstrap class path not set in conjunction with -source 1.7, because it might generate error co compile against a different version of the JCL.

So you would need to set rt.jar with:

javac -source 1.7 -bootclasspath /usr/lib/jvm/java-7-oracle/jre/lib/rt.jar Main.java

This was asked at: warning: [options] bootstrap class path not set in conjunction with -source 1.5


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.