I am looking for a general inverse equivalent of the ShutdownHook in Java Runtime i.e. something like a StartupHook where certain custom warmup or setup code can be executed when the JVM first starts up.
I am aware of alternatives like the use of ServletContexts etc. on the startup of Servlet Containers, or such similar features in other frameworks like Spring etc. But these are not what I'm looking for. I am looking for a general JVM solution, if one is available.
Please let me know if the whole idea of a StartupHook would be inadvisable for the JVM and why.
Update: After going through all the answers provided (thanks everyone), it seems that the closest to what I'm looking for is Java Agents.
Although it would be nice (from an ease-of-use point of view but probably not from a security point of view) if the JVM allows me to do something like this:
- Write a class that implements StartUpHook with only one method - void preMain()
- Put that class into a jar file and put that jar into the classpath or the JRE extensions directory
- When JVM starts up, it looks for all classes in the classpath that implements StartUpHook and then calls its preMain() method.