2

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.

  • Technically yes, it's possible. But it involves advanced things, so why do you think you need to modify the standard JDK classes? – Kayaman Sep 11 '19 at 12:36
  • You can download the JDK source code and play with it: https://stackoverflow.com/questions/2896727/where-to-find-java-jdk-source-code – rbento Sep 11 '19 at 12:38
  • 3
    A person who is planning to modify JDK classes is not supposed to be confused about where to find the source code ;) – XBlueCode Sep 11 '19 at 12:49
  • Can someone please write the complete steps or make a tutorial on YouTube – Pari_programmer Sep 11 '19 at 15:21

3 Answers3

3

Better approach would be extending those Class and modifying the specific methods. This will not affect other project where you just wanted Java provided Class

WorkaroundNewbie
  • 1,063
  • 9
  • 16
3

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.

Community
  • 1
  • 1
Lesiak
  • 22,088
  • 2
  • 41
  • 65
0

Files in that jar are compiled .class files from .java source files. You cannot modify the binaries directly that easy, but if you have a source code (e.g. https://github.com/openjdk/) you can compile your own .class files and replace, since JAR is basically an archive with binaries.

Not the most scalable approach, but possible.

Kudin
  • 456
  • 4
  • 7