Why do we use rt.jar in a java project?
JavaJava Problem Overview
What is the necessity of rt.jar ??
Java Solutions
Solution 1 - Java
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.
Solution 2 - Java
rt = Run Time
It contains all the java runtime libraries. (Essential)
Solution 3 - Java
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: https://stackoverflow.com/questions/7816423/warning-options-bootstrap-class-path-not-set-in-conjunction-with-source-1-5/29384628#29384628
Solution 4 - Java
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.
Solution 5 - Java
It contains the Java built-in classes. rt
maybe stands for Runtime
. Without it you couldn't run Java
programs:)
Solution 6 - Java
The rt.jar is where all the java packages reside. For example, if a class file calls for the java.util package, then the JVM can look for it inside the rt.jar, thus enabling it to run correctly.
On a side note: Don't mess around with it.
Solution 7 - Java
It contains all the standard JDK classes. During the process of class loading in JVM, this is the first one to get loaded and is done by the bootstrap class loader, parent of all class loaders.
You can check it yourself by compiling a java program with this option:
javac -verbose program.java
in order to see the sequence of the class loaded.
Sample:
[Loaded sun.security.timestamp.TimestampToken from /usr/lib/jvm/java-8-oracle/jre/lib/rt.jar]
[Loaded sun.security.util.CertConstraintParameters from /usr/lib/jvm/java-8-oracle/jre/lib/rt.jar]
[Loaded sun.security.util.ECKeySizeParameterSpec from /usr/lib/jvm/java-8-oracle/jre/lib/rt.jar]
[Loaded sun.security.util.ECUtil from /usr/lib/jvm/java-8-oracle/jre/lib/rt.jar]
[Loaded sun.security.util.Pem from /usr/lib/jvm/java-8-oracle/jre/lib/rt.jar]
Solution 8 - Java
The runtime (rt.jar) holds all the (most of the..) java classes that form the Java SE. It is added to the classpath automatically.
Solution 9 - Java
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
-
rt.jar stands for runtime and contains all of the compiled class files for the core Java Runtime environment.
- 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
- 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
-
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.
-
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.
-
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.
-
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.