I have a Java program that loads thirdparty class files (classes I did not write) and executes them. These classes often use java.util.Random
, which by default generates random starting seed values every time it gets instantiated. For reasons of reproducability, I want to give these classes the same starting seed every time, changing it only at my discretion.
Here are some of the obvious solutions, and why they don't work:
Use a different Random class in the thirdparty classfiles. The problem here is I only load the class files, and cannot modify the source.
Use a custom classloader to load our own Random class instead of the JVM's version. This approach will not work because Java does not allow classloaders to override classes in the
java
package.Swap out the rt.jar's
java.util.Random
implementation for our own, or putting files into trusted locations for the JVM. These approaches require the user of the application messing with the JVM install on their machine, and are no good.Adding a custom
java.util.Random
class to the bootclasspath. While this would technically work, for this particular application, it is impractical because this application is intended for end users to run from an IDE. I want to make running the app convenient for users, which means forcing them to set their bootclasspath is a pain. I can't hide this in a script, because it's intended to be run from an IDE like Eclipse (for easy debugging.)
So how can I do this?