Patch or override an implementation of a core Java 10 class

烈酒焚心 提交于 2019-12-06 18:19:55

问题


There is a bug in JFX which often manifests when calculating screen co-ordinates https://bugs.openjdk.java.net/browse/JDK-8194727 and https://bugs.openjdk.java.net/browse/JDK-8190400

I've tracked the problem down to the implementation of GeneralTransform3D, which is part of the javajfx runtime.

I've submitted a bug report to Oracle, but until it is accepted, fixed, and makes it to a release, I need a way of fixing my application.

In java 8 i was able to create a jar containing a fixed version of the class and install it in the lib/ext folder. This seemed to work and the JFX implementation used my impl over its own.

In java 10 the extension mechanism has been removed. Adding the patch jar to the classpath doesn't work as it is too late in the classloading process.

Is there a way to override/patch an implementation of the core java classes in Java 10?

Note that i'm not using this class directly, it is used by the framework


回答1:


Once again, Alan gives the best answer as a comment. :) Quote:

--patch-module javafx.runtime=patch.jar is the right way to override classes in this module

If you need to "override" a class in a platform module, use --patch-module to do that. If that drags in additional dependencies, make sure to make them readable with --add-reads.




回答2:


Looks like a solution is possible using java agents, as per this question

Replace a class within the Java class library with a custom version




回答3:


I needed to do this but I was launching Java from C through the JNI interface (instead of the command line). Just transposing the command line args to JavaVMOptions didn't work. Instead it all goes in one arg as follows:

JavaVMOption options[N_ARGS] = { 0 };
options[0].optionString = "--patch-module=javafx.runtime=patch.jar";

It took a lot of digging to figure this out, so hope it saves someone else some time.



来源:https://stackoverflow.com/questions/50827263/patch-or-override-an-implementation-of-a-core-java-10-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!