Explain Codes LogoExplain Codes Logo

How to create a windows service from java app

java
wrapper
service-wrapper
java-service
Alex KataevbyAlex Kataev·Feb 16, 2025
TLDR

Less talk, more action. Convert your Java app into a Windows service in a snap using Apache Commons Daemon's prunsrv. Roll up your sleeves and punch in this command:

prunsrv.exe //IS//MyService --Install=myAppPath --Jvm=auto --StartMode=jvm --StartClass=YourMainClass --StartMethod=startService --StopMode=jvm --StopClass=YourMainClass --StopMethod=stopService

Swap myAppPath with your app's location, YourMainClass with your main class. We need startService and stopService methods for handling your service's lifecycle. Control it using prunsrv.exe for start, stop, delete.

Tools at your disposal

Let's talk wrappers - not the kind you eat. Apache Commons Daemon, WinRun4J, NSSM, and YAJSW will get the job done. Procrun from Apache Commons Daemon is your go-to for Windows, while Jsvc caters to Unix lovers.

Guide to bootstrappin'

Can't change the existing methods in your application? Fear not. Create a bootstrapper class to take the reins and handle start and stop calls coming from Procrun.

Customization and control

With great power comes great customizability. WinRun4J lets you assign custom icons and keep an eye on your service in the system tray itself.

Preppin’ your Java app for service glory

Ready to promote your app from employee to service? Let's get started:

  • Outfit your app in an elegant .jar suit.
  • Grab Procrun and set it up to call your .jar whenever duty calls.
  • Needing inspiration? Take a cue from Tomcat or dive into Java Service Wrapper or WinRun4J.

Choosing the sturdiest horse for the long run

Future-proof your service with a wrapper that’s both dependable and sustainable:

  • YAJSW on GitHub—robust, regularly updated.
  • WinRun4J—offering monitoring features as cherries on the cake.