javaoopjava-6

How can we edit Java built-in classes such as Java.util.Scanner?


Just for the sake of curiosity , I want to know is there a way I can edit/modify Java core classes inside rt.jar. I am searching everywhere in the internet but got no relevant answers. I have also referred to Covert Java(book) but I am unable to understand it.

Note: I don't want to distribute my app. I just want to do it for my curiosity.


Solution

  • Another answer suggested patching rt.jar I believe this is not the best approach - you effectively cannot distribute your app (unless you have your app contenerized and you deliver Java with your app).

    The way to go is to learn about bootstrap classpath This should cover most of reasonable use cases.

    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.