I have a core java application running on a server. When I start the application from main, I want a another thread to be started automatically, without being explicitly called from main. It's like I need a Thread to be started when I start the JVM.
I want to put application monitoring code in that thread and make it into a jar file. By putting this jar file into the classpath of any application I want to be able to monitor them.
Is this possible???
They say you have to be the first, the best or different. I say, is it too much to ask for all three.
Why would they? Class Monitor is never used, and thus never loaded or instantiated.
I know that Ulf , I was hoping this could be achieved by some roundabout coding.
What do you mean by this? Any background thread you want to run will need to be created and started explicitly.
I want to write a thread that can be put in a jar file and by keeping this jar in the classpath of any application, the thread should be started, when the JVM is launched. This is needed so that this jar can be used without changing any code in the actual application.
Joined: Mar 22, 2005
Code doesn't get executed unless something causes it to do so. If this were an JEE or servlet container, then there are ways of running code at server startup (servlet context initializers and @Startup beans).
It doesn't look like what he wants is J2EE , and like you say unless he causes Monitor's class loader to be invoked the static initialiser won't get called. I presume there is a reason why you just can't change main and have your thread creation based on application parameter or invoke monitor and have a dummy implementation you could replace in if you didn't want thread creation ?