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.
It contains the Java built-in classes. rt
maybe stands for Runtime
. Without it you couldn't run Java
programs:)
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.
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]
The runtime (rt.jar) holds all the (most of the..) java classes that form the Java SE. It is added to the classpath automatically.
精彩评论