Replace class in Java class library with custom version

The BasicLabelUI class in javax / swing / plaf / basic depends on the confirmed error . In my application, I need the functionality provided by the fixed version (registered for v9) . For legal and technical reasons, I'm still attached to the affected version of the JDK.

My approach was to create a javax / swing / plaf / basic package inside my project containing a fixed version.

How can I get my project to approve my included version of a class over a defective class in an installed JDK?

This should be somewhat portable, as the fixed class should also work on the client side, and the defective class in the JDK installation should be ignored. Therefore, I do not want to change the JDK, but rather bypass this particular class.

+8
java swingx patch
source share
2 answers

As mentioned in other answers, you could in theory, of course, unzip your rt.jar JVM file and replace the file with a compatible version with error correction.

Any Java class library classes, such as Swing, are loaded by the bootstrap class loader, which looks for its classes from this rt.jar. You can usually not add classes to this class path without adding them to this file. There is a (non-standard) VM option

-Xbootclasspath/jarWithPatchedClass.jar:path 

where you added a jar file that includes a patched version, but this does not necessarily work on any Java virtual machine. It is also illegal to deploy an application that modifies this behavior. As stated in the official documentation :

Do not deploy applications that use this parameter to override the class in rt.jar, as this violates the binary code of the Java Runtime Environment license.

If you add a class to the bootstrap class loader (which is possible without using non-standard APIs using the toolkit API), the runtime will still load the source class as the bootloader class bootloader, in this case it searches for rt.jar first. Therefore, it is impossible to “obscure” a broken class without modifying this file.

Finally, it is always illegal to distribute a virtual machine with a fixed file , i.e. put it in the production system for the client. The license agreement clearly states what you need

[...] distribute [Java runtime] fully and unmodified and only bundled with your applets and applications

Therefore, modifying the VM that you distribute is not recommended as you may face legal consequences when it is ever revealed.

Of course, you can theoretically build your own version of OpenJDK, but you can no longer invoke binary Java when you distribute it, and I assume that your client would not allow this by what you offer in your answer. From experience, many secure environments compute hashes of binary files before execution, to prohibit both approaches to setting up a running virtual machine.

The easiest solution for you is probably to create

The javadoc java.lang.instrumentation offers a detailed description of how to create and implement a Java agent. Using this approach, you can use a fixed version of the class in question without violating the license agreement .

From experience, Java agents are a great way to fix temporary errors in third-party libraries and in the Java class library without having to deploy code changes or even deploy a new version for the client. This is actually a typical example of using a Java agent.

+11
source share

How can I get my project to approve my included version of a class over a defective class in an installed JDK?

The simple answer is you cannot. At least not while strictly abiding by the restriction that you must use the affected version of Javs.

Assuming that you can determine the appropriate version in the original OpenJDK repositories, you can create your own flavor of the Java libraries with the bug fixed. However, this will not be real Java. Of course, he will not qualify as an “affected version of Java,” which you are limited to use. (And besides, you devote yourself to an endless cycle of reapplying the patch to each new release of the patch of the current version of Java ...)

It is also theoretically possible to put a modified version of some standard Java library class in the JAR and add it to the bootstrap JTM class using the -Xbootclasspath command-line -Xbootclasspath . But this is tantamount to changing the "affected version of Java."

Doing this using a Java agent to use a revised version of the class also violates the rules. And it’s more complicated.


If you and your clients have decided that configuring the JVM is an acceptable solution, then doing this via the bootstrap class path is probably the easiest and cleanest approach. AND DEFINITELY legal 1 .

However, I would recommend that you find a workaround for the error until the corrected version of Java 9 is released.


1 - In fact, even a modified-source build approach is legal, as the Oracle Binary license does not apply to this. The binary license is to distribute a modified version of the Oracle binary. Another possible problem is that you may violate the Java trademark terms if you distribute a version that is incompatible with “true” Java and invoke your “Java” distribution. The solution to this ... do not call it "Java"!

However, do not follow my advice. Ask a lawyer. Better yet, do not do this at all. This is unnecessarily complicated.

0
source share

All Articles