Search code examples
javarandomjvmclassloader

How can I give java.util.Random a specific seed in thirdparty classes?


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:

  1. Use a different Random class in the thirdparty classfiles. The problem here is I only load the class files, and cannot modify the source.

  2. 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.

  3. 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.

  4. 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?


Solution

  • Consider modifying the third party libraries to have them use a seen for their Random instances. Though you do not have the source code, you can probably edit the bytecode to do it. One helpful toolkit for doing such is ASM.