为什么我们在java项目中使用rt.jar?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3091040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why do we use rt.jar in a java project?
提问by Chetan
What is the necessity of rt.jar ??
rt.jar 的必要性是什么??
采纳答案by Chathuranga Chandrasekara
rt = Run Time
rt = 运行时间
It contains all the java runtime libraries. (Essential)
它包含所有的 Java 运行时库。(基本的)
回答by Thorbj?rn Ravn Andersen
It contains all the classes provided in the Java Runtime Environment.
它包含在Java提供的所有类[R未牛逼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。
回答by Andreas Dolk
The runtime (rt.jar) holds all the (most of the..) java classes that form the Java SE. It is added to the classpath automatically.
运行时 (rt.jar) 包含构成 Java SE 的所有(大部分..)Java 类。它会自动添加到类路径中。
回答by Petar Minchev
It contains the Java built-in classes. rt
maybe stands for Runtime
. Without it you couldn't run Java
programs:)
它包含 Java 内置类。rt
也许代表Runtime
. 没有它你就不能运行Java
程序:)
回答by Awesomedude8888
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.
rt.jar 是所有 java 包所在的地方。例如,如果类文件调用 java.util 包,则 JVM 可以在 rt.jar 中查找它,从而使其能够正确运行。
On a side note: Don't mess around with it.
附带说明:不要乱搞。
回答by Shubham Verma
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.
rt.jar 代表运行时 JAR 并包含引导程序类,我的意思是来自 Core Java API 的所有类。我发现很多Java程序员不知道rt.jar是什么?并且经常与 rt.jar 文件的作用或我们为什么在 Java 中使用 rt.jar 文件混淆?毫不奇怪,这个名字有点神秘。
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.
该文件始终位于 JRE 的 lib 目录中,至少在 Windows 和 Linux 中是这样。在 MacOSX 中,它位于不同的位置,也有不同的名称,即 classes.jar,但这只是在 JDK 1.7 之前。从 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. 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 中以解决与类路径相关的问题,但这是一个坏主意。你永远不应该弄乱 rt.jar,它包含 JVM 信任的类文件,并且在没有严格的安全检查的情况下加载它对其他类文件所做的。
回答by Sudip Bhandari
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.
它包含所有标准的 JDK 类。在 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]
回答by Mugesh Kanagu
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.
rt.jar 代表运行时 JAR 并包含引导程序类,我的意思是来自 Core Java API 的所有类。我发现很多Java程序员不知道rt.jar是什么?并且经常与 rt.jar 文件的作用或我们为什么在 Java 中使用 rt.jar 文件混淆?毫不奇怪,这个名字有点神秘。该文件始终位于 JRE 的 lib 目录中,至少在 Windows 和 Linux 中是这样。在 MacOSX 中,它位于不同的位置,也有不同的名称,即 classes.jar,但这只是在 JDK 1.7 之前。从 Java 7 版本开始,Apple 已停止分发 Java,如果您单独安装,它将与 rt.jar 具有相同的名称。许多开发人员认为将他们的类包含在 rt.jar 中以解决与类路径相关的问题,但这是一个坏主意。你永远不应该弄乱 rt.jar,它包含 JVM 信任的类文件,并且在加载时没有对其他类文件进行严格的安全检查。在本文中,我们将从 Java 世界中了解有关这个神奇 JAR 的一些有趣的事情。对于那些刚接触 Java 并且不熟悉 JAR 文件的程序员来说,它是一个类似 zip 的文件,确切地说是 Java 归档文件,它存储 Java 类文件和程序所需的任何资源。它还可以包含 mainfest 文件,该文件可以包含 Main-Class 条目以使其成为可执行 JAR,可以使用 java -jar 命令运行该 JAR。准确地称为 Java 存档,它存储 Java 类文件和程序所需的任何资源。它还可以包含 mainfest 文件,该文件可以包含 Main-Class 条目以使其成为可执行 JAR,可以使用 java -jar 命令运行该 JAR。准确地称为 Java 存档,它存储 Java 类文件和程序所需的任何资源。它还可以包含 mainfest 文件,该文件可以包含 Main-Class 条目以使其成为可执行 JAR,可以使用 java -jar 命令运行该 JAR。
Important Points about rt.jar in Java
Java中关于rt.jar的要点
- rt.jar stands for runtime and contains all of the compiled class files for the core Java Runtime environment.
- rt.jar 代表运行时,包含为核心 Java 运行时环境编译的所有类文件。
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.
2) 您必须在类路径中包含 rt.jar,否则您将无法访问核心类,例如 java.lang.String、java.lang.Thread、java.util.ArrayList 或 java.io.InputStream 以及所有其他类来自 Java API。通过使用 WinRAR 或 WinZip 客户端打开 rt.jar,您实际上可以看到其中的内容。可以看到,它不仅包含了所有的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. 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.
3)在windows下,rt.jar会一直驻留在$JAVA_HOME/jre/lib下,其中$JAVA_HOME指的是JDK的安装目录。即使你不安装JDK,只安装JRE,你也会在完全相同的位置看到它,你不会在$JAVA_HOME/lib 目录中找到rt.jar。顺便说一句,在 MacOSX 上,它被称为 classes.jar,位于 /System/Library/Frameworks//Classes 目录下。在以下屏幕截图中,您可以看到 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. 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.
4) rt.jar 是所有 Java 包所在的地方。例如,如果一个类文件需要从 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? 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.
5) Java 程序员问的另一个问题是,我在哪里可以找到 rt.jar 中包含的类的源代码?好吧,如果您安装了 JDK,而不是 JRE,那么您可以在 $JAVA_HOME/src.zip 文件中找到所有源代码。顺便说一句,sun.* 源也包含在 src.zip 中,但这是专有的封闭源 Oracle 代码。我还建议您在 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. 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.
6) 了解 rt.jar 的最重要的事情之一是 JVM 知道此 JAR 文件中的所有类,这意味着 JVM 在从任何其他位置加载任何其他 JAR 时不会执行它所做的所有检查。这是由于各种性能原因而完成的,这就是为什么这些类由引导程序或原始类加载器加载的原因。不要尝试在 rt.jar 中包含您的类文件,因为 Java 不建议这样做。它还会损害任何安全性。
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.
7) 如果您对 Java 平台使用的不同二进制文件和 JAR 文件感到好奇,请查看此图表。可以看到JDK有三个主要文件夹bin、lib和jre。bin 目录包含所有二进制可执行文件,例如运行 Java 程序的 java.exe,编译 Java 程序的 javac.exe 等。lib 包含 tools.jar 和 dt.jar。jre 文件夹再次包含 bin 和 lib 目录。它在这个lib目录下rt.jar驻留。顺便说一下,有关这些文件和文件夹的功能的完整说明,请查看 Oracle 官方页面。它们非常全面且具有描述性。
basic folders and files in JDK JRE
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.
这就是 Java 中的 rt.jar 文件的全部内容。现在您知道 rt.jar 的目的是什么以及为什么不应该弄乱它。您可以在 $JAVA_HOME/jre/lib 目录中找到这个 JAR 文件,我鼓励您自己查看。
阅读更多:https: //javarevisited.blogspot.com/2015/01/what-is-rtjar-in-javajdkjre-why-its-important.html#ixzz5icL7sAMZ