简体   繁体   English

为什么我们在java项目中使用rt.jar?

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

rt.jar的必要性是什么?

It contains all the classes provided in the Java R un t ime Environment. 它包含Java R un t ime环境中提供的所有类。

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. 如果在类路径中没有它,则无法访问需要使用的任何类,如java.lang.String或java.io.File。

rt = Run Time rt =运行时间

It contains all the java runtime libraries. 它包含所有Java运行时库。 (Essential) (必要)

Cross compilation is one case where you have to explicitly use it. 交叉编译是一种必须明确使用它的情况。

Eg, if you are on Java 8, and want to compile Java 7 while rejecting Java 8 extensions. 例如,如果您使用的是Java 8,并希望在拒绝Java 8扩展时编译Java 7。 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. 但是后来javac会说: warning: [options] bootstrap class path not set in conjunction with -source 1.7 ,因为它可能会针对不同版本的JCL生成错误co编译。

So you would need to set rt.jar with: 所以你需要设置rt.jar

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 这被问到: 警告:[options] bootstrap类路径未与-source 1.5一起设置

rt.jar stands for runtime JAR and contains the bootstrap classes, I mean all the classes from Core Java API. rt.jar代表运行时JAR并包含引导类,我的意思是来自Core Java API的所有类。 I have found that many Java programmer doesn't know what is rt.jar? 我发现很多Java程序员都不知道什么是rt.jar? and often confused with the role of rt.jar file or why we use of rt.jar file in Java? 并经常混淆rt.jar文件的作用或为什么我们在Java中使用rt.jar文件? No surprise, the name is little bit cryptic. 毫不奇怪,这个名字有点神秘。

This file always reside inside lib directory of JRE, at least in Windows and Linux. 此文件始终位于JRE的lib目录中,至少在Windows和Linux中。 In MacOSX it reside at different location and also has different name ie classes.jar, but that is only prior to JDK 1.7. 在MacOSX中,它位于不同的位置,并且具有不同的名称,即classes.jar,但这仅在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. 从Java 7版本开始,Apple已停止分发Java,如果单独安装,它将与rt.jar同名。

Many developer thinks to include their classes inside rt.jar to solve classpath related problems, but that is a bad idea. 许多开发人员都认为将他们的类包含在rt.jar中以解决与类路径相关的问题,但这是一个坏主意。 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. 你永远不应该弄乱rt.jar,它包含JVM信任的类文件,并且加载时没有对其他类文件进行严格的安全检查。

It contains the Java built-in classes. 它包含Java内置类。 rt maybe stands for Runtime . rt可能代表Runtime Without it you couldn't run Java programs:) 没有它你就无法运行Java程序:)

The rt.jar is where all the java packages reside. rt.jar是所有java包所在的位置。 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. 例如,如果类文件调用java.util包,那么JVM可以在rt.jar中查找它,从而使其能够正确运行。

On a side note: Don't mess around with it. 旁注:不要乱用它。

The runtime (rt.jar) holds all the (most of the..) java classes that form the Java SE. 运行时(rt.jar)包含构成Java SE的所有(大多数..)java类。 It is added to the classpath automatically. 它会自动添加到类路径中。

It contains all the standard JDK classes. 它包含所有标准JDK类。 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. 在JVM中加载类的过程中,这是第一个加载的加载过程,由引导类加载器(所有类加载器的父加载器)完成。

You can check it yourself by compiling a java program with this option: 您可以通过使用此选项编译java程序来自行检查:

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]

rt.jar stands for runtime JAR and contains the bootstrap classes, I mean all the classes from Core Java API. rt.jar代表运行时JAR并包含引导类,我的意思是来自Core Java API的所有类。 I have found that many Java programmer doesn't know what is rt.jar? 我发现很多Java程序员都不知道什么是rt.jar? and often confused with the role of rt.jar file or why we use of rt.jar file in Java? 并经常混淆rt.jar文件的作用或为什么我们在Java中使用rt.jar文件? No surprise, the name is little bit cryptic. 毫不奇怪,这个名字有点神秘。 This file always reside inside lib directory of JRE, at least in Windows and Linux. 此文件始终位于JRE的lib目录中,至少在Windows和Linux中。 In MacOSX it reside at different location and also has different name ie classes.jar, but that is only prior to JDK 1.7. 在MacOSX中,它位于不同的位置,并且具有不同的名称,即classes.jar,但这仅在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. 从Java 7版本开始,Apple已停止分发Java,如果单独安装,它将与rt.jar同名。 Many developer thinks to include their classes inside rt.jar to solve classpath related problems, but that is a bad idea. 许多开发人员都认为将他们的类包含在rt.jar中以解决与类路径相关的问题,但这是一个坏主意。 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. 你永远不应该弄乱rt.jar,它包含JVM信任的类文件,并且加载时没有对其他类文件进行严格的安全检查。 In this article, we will learn some interesting things about this magical JAR from Java world. 在本文中,我们将从Java世界中学习关于这个神奇的JAR的一些有趣的事情。 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. 对于那些刚接触Java并且不熟悉JAR文件的程序员来说,它是一个类似zip的文件,精确称为Java archive,它存储Java类文件和程序所需的任何资源。 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. 它还可以包含mainfest文件,该文件可以包含Main-Class条目,使其成为可执行JAR,可以使用java -jar命令运行。

Important Points about rt.jar in Java 关于Java中rt.jar的重要观点

  1. rt.jar stands for runtime and contains all of the compiled class files for the core Java Runtime environment. rt.jar代表运行时,包含核心Java Runtime环境的所有已编译类文件。

2) You must include rt.jar in your classpath, otherwise you don't have access to core classes eg java.lang.String, java.lang.Thread, java.util.ArrayList or java.io.InputStream and all other classes from Java API. 2)您必须在类路径中包含rt.jar,否则您无权访问核心类,例如java.lang.String,java.lang.Thread,java.util.ArrayList或java.io.InputStream以及所有其他类来自Java API。 You can actually see what is inside rt.jar by opening it by using WinRAR or WinZip client. 您可以通过使用WinRAR或WinZip客户端打开它来实际查看rt.jar中的内容。 You can see that it not only contains all Java API but also internal classes specified in com package. 您可以看到它不仅包含所有Java API,还包含com包中指定的内部类。

What is rt.jar in Java 什么是Java中的rt.jar

3) In windows, rt.jar will always reside under $JAVA_HOME/jre/lib, where $JAVA_HOME refers to JDK installation directory. 3)在windows中,rt.jar将始终位于$ JAVA_HOME / jre / lib下,其中$ JAVA_HOME指的是JDK安装目录。 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. 即使您没有安装JDK并且只安装JRE,您也会在完全相同的位置看到它,在$ JAVA_HOME / lib目录中找不到rt.jar。 BTW, On MacOSX it is called classes.jar and located under /System/Library/Frameworks//Classes directory. BTW,在MacOSX上,它被称为classes.jar,位于/ System / Library / Frameworks // Classes目录下。 In following screenshot you can see that rt.jar is inside JRE's lib directory in Windows 8. 在下面的屏幕截图中,您可以看到rt.jar位于Windows 8中JRE的lib目录中。

Where to find rt.jar in Windows, Linux and MacOSX 哪里可以在Windows,Linux和MacOSX中找到rt.jar

4) The rt.jar is where all the Java packages reside. 4)rt.jar是所有Java包所在的位置。 For example, if a class file need to refer a class from java.util.concurrent package eg ConcurrentHashMap, then the JVM will look for it inside the rt.jar, thus enabling it to run correctly. 例如,如果类文件需要从java.util.concurrent包中引用类,例如ConcurrentHashMap,那么JVM将在rt.jar中查找它,从而使其能够正确运行。

5) One more question Java programmer ask is, where can I find source code for classes included in rt.jar? 5)Java程序员问的另一个问题是,在哪里可以找到rt.jar中包含的类的源代码? well, if you have installed JDK, not JRE then you can find all sources inside $JAVA_HOME/src.zip file. 好吧,如果您已经安装了JDK而不是JRE,那么您可以在$ JAVA_HOME / src.zip文件中找到所有源代码。 BTW, sun.* sources are also included in src.zip but that is proprietary closed source Oracle code. BTW,sun。*源也包含在src.zip中,但这是专有的闭源Oracle代码。 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. 我还建议你在Eclipse中包含这个JAR文件,这样你只需键入Ctrl + T和类的名称就可以查看任何JDK类的源代码,其余部分将由Eclipse的Java类型搜索功能处理。

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. 6)关于rt.jar最重要的一点是,JVM知道这个JAR文件中的所有类,这意味着JVM在从任何其他位置加载任何其他JAR时不会执行所有检查。 This is done due to various performance reason and that's why these classes are loaded by bootstrap or primodial class loaders. 这是由于各种性能原因而完成的,这就是为什么这些类由bootstrap或primodial类加载器加载的原因。 Don't try to include your class files in rt.jar, as its not advised by Java. 不要尝试将您的类文件包含在rt.jar中,因为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. 7)如果您对Java平台使用的不同二进制文件和JAR文件感到好奇,请查看此图。 You can see that JDK has three main folders bin, lib and jre. 您可以看到JDK有三个主文件夹bin,lib和jre。 bin directory contains all binary executable eg java.exe to run Java program, javac.exe to compile Java program etc. lib contains tools.jar and dt.jar. bin目录包含所有二进制可执行文件,例如java.exe运行Java程序,javac.exe编译Java程序等.lib包含tools.jar和dt.jar。 jre folder again contain bin and lib directory. jre文件夹再次包含bin和lib目录。 It's in this lib directory rt.jar reside. 它位于这个lib目录rt.jar驻留中。 By the way for complete explanation of what each of these file and folder does, checkout Oracle official pages. 顺便说一下,为了完整解释每个文件和文件夹的功能,请查看Oracle官方页面。 They are very comprehensive and descriptive. 它们非常全面和具有描述性。

basic folders and files in JDK JRE JDK JRE中的基本文件夹和文件

That's all about rt.jar file in Java. 这都是关于Java中的rt.jar文件。 Now you know what is the purpose of rt.jar and why you should not mess with it. 现在你知道rt.jar的目的是什么,为什么你不应该惹它。 You can find this JAR file inside $JAVA_HOME/jre/lib directory and I encourage you to look it by yourself. 您可以在$ JAVA_HOME / jre / lib目录中找到此JAR文件,我建议您自己查看。

Read more: https://javarevisited.blogspot.com/2015/01/what-is-rtjar-in-javajdkjre-why-its-important.html#ixzz5icL7sAMZ 阅读更多: https//javarevisited.blogspot.com/2015/01/what-is-rtjar-in-javajdkjre-why-its-important.html#ixzz5icL7sAMZ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM