我们如何编辑Java内置类如Java.util.Scanner?
How can we edit Java built-in classes such as Java.util.Scanner?
出于好奇,我想知道有没有办法可以在 rt.jar 中 edit/modify Java 核心 类。我在互联网上到处搜索,但没有得到相关答案。我也提到了 Covert Java(book) 但我无法理解它。
注意:我不想分发我的应用程序。我只是出于好奇才想做的。
该 jar 中的文件是从 .java 个源文件编译的 .class 个文件。您不能那么容易地直接修改二进制文件,但是如果您有源代码(例如 https://github.com/openjdk/),您可以编译自己的 .class 文件并替换,因为 JAR 基本上是二进制文件的存档。
不是最具扩展性的方法,但可能。
更好的方法是扩展那些 Class 并修改特定方法。这不会影响您只想要 Java provided Class
的其他项目
另一个答案建议修补 rt.jar
我认为这不是最好的方法 - 您实际上无法分发您的应用程序(除非您已将您的应用程序竞争并交付 Java 与您的应用程序)。
要走的路是了解 bootstrap classpath
这应该涵盖大多数合理的用例。
The normal Java classloaders look for classes first in the bootstrap classpath, before checking for extensions and the application classpath. By default, the bootstrap classpath consists of the "rt.jar" file and some other important JAR files that are supplied by the JRE installation. These provide all of the classes in the standard Java SE class library, along with various "internal" implementation classes.
Under normal circumstances, you don't need to concern yourself with this. By default, commands like java, javac and so on will use the appropriate versions of the runtime libraries.
Very occasionally, it is necessary to override the normal behavior of the Java runtime by using an alternative version of a class in the standard libraries. For example, you might encounter a "show stopper" bug in the runtime libraries that you cannot work around by normal means. In such a situation, it is possible to create a JAR file containing the altered class and then add it to the bootstrap classpath which launching the JVM.
出于好奇,我想知道有没有办法可以在 rt.jar 中 edit/modify Java 核心 类。我在互联网上到处搜索,但没有得到相关答案。我也提到了 Covert Java(book) 但我无法理解它。
注意:我不想分发我的应用程序。我只是出于好奇才想做的。
该 jar 中的文件是从 .java 个源文件编译的 .class 个文件。您不能那么容易地直接修改二进制文件,但是如果您有源代码(例如 https://github.com/openjdk/),您可以编译自己的 .class 文件并替换,因为 JAR 基本上是二进制文件的存档。
不是最具扩展性的方法,但可能。
更好的方法是扩展那些 Class 并修改特定方法。这不会影响您只想要 Java provided Class
的其他项目另一个答案建议修补 rt.jar 我认为这不是最好的方法 - 您实际上无法分发您的应用程序(除非您已将您的应用程序竞争并交付 Java 与您的应用程序)。
要走的路是了解 bootstrap classpath 这应该涵盖大多数合理的用例。
The normal Java classloaders look for classes first in the bootstrap classpath, before checking for extensions and the application classpath. By default, the bootstrap classpath consists of the "rt.jar" file and some other important JAR files that are supplied by the JRE installation. These provide all of the classes in the standard Java SE class library, along with various "internal" implementation classes.
Under normal circumstances, you don't need to concern yourself with this. By default, commands like java, javac and so on will use the appropriate versions of the runtime libraries.
Very occasionally, it is necessary to override the normal behavior of the Java runtime by using an alternative version of a class in the standard libraries. For example, you might encounter a "show stopper" bug in the runtime libraries that you cannot work around by normal means. In such a situation, it is possible to create a JAR file containing the altered class and then add it to the bootstrap classpath which launching the JVM.