When I first developed a Java service for Windows using Apache daemon, I used the JVM mode which I liked a lot. You specify your class and start\stop (static) methods. But with Linux, Jsvc doesn’t look like it has the same option. I would really like to know why ?!
Anyway If I’m going to use Linux’s init system, I’m trying to find a similar way to accomplish the same behavior which is to start the app in anyway but to stop it, I’ll have to call a method in a class.
My question is, after the jar is started, how can I use the JVM libraries or anything else, to call a method in my application (which will attempt to stop my application gracefully).
Another side question, if an application is started and that application has static methods, If I use the java command line to run a main method in one if that’s application class, and the main method, which is static would call another static method in the class in which I would like to signal the termination signal, would that call by in the same JVM?
Why not rather add a
ShutdownHookto your application?This will allow your jar to terminate gracefully before being shutdown:
It is important to note
The shutdown hook runs when:
The shutdown hook will not run when:
Alternatively if you must you can use this to call a method in a class:
and to load a class dynamically:
References: