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:
There is no startup hook in Java. Your choices are (in the order I would recommend them).
main(String[])
method with your own main(String[])
method and then delegate after calling your main from the commandline.Premain-Class
definition in your jar manifest then add the agent to the JVM on the command line.The last two options require you to add or changes things on the command-line.