A problem sometimes encountered by a Java programmer, is to make your Java program into a Windows Service. This is may be a bump in your project, particularly if you don’t know anything about windows services, or much about windows for that matter.

The demo created a running, working, Windows service server using 14 lines of Java code, and some maven configuration.

Before starting on the demo, a few words on what windows services are (from a GNU/linux/UNIX perspective):

Windows services are the “daemons” of the windows world

Windows services are normally started when the windows system starts, and stopped when the windows system shuts down

Windows services can be stopped and started by administrator users, both using a GUI and using command line commands

Windows services can be configured to run with a particular user, restricting what the service can do (default is the local user “Local System”)

The Java program turned into a windows service during the demo, is the Wiser test SMTP server. Wiser was picked, because:

It has an appropriate API

An SMTP service is easy to demonstrate, and it is something other than yet another HTTP service

Since the demo might be hard to follow (a lot of information in 10 minutes), this blog post describes all steps of the demo (note: the complete code can be found on github at https://github.com/sbang/ansmtpserver ).

In the constructor, create an Wiser instance, set the host name, and the port number

In the startDaemon() method start the Wiser server

In the stopDaemon() method stop the Wiser server

Save the modified AnSmtpServer.java file

Right-click ansmtpserver->src/main/resources in the “Package Explorer” and select New->File

In the “New File” dialog box

In “File name”, type

simplelogger.properties

Click the “Finish” button

Modify the “simplelogger.properties” file to have the following content

org.slf4j.simpleLogger.defaultLogLevel=debug

and save the file

Select the “ansmtpserver/pom.xml” editor, and select the “pom.xml” tab, and paste the following before the </project> end tag. This configuration will be the same for all installers with the exception of the <prunsrvDaemonLauncherClass> tag

Open ansmtpserver->src/main/java->ansmtpserver->AnSmtpServer.java in the “Package Explorer”, right-click the “AnSmtpServer” class, and select “Copy Qualified Name” and paste the name into the <prunsrvDaemonLauncherClass> element

Save the pom.xml file

Open a cmd.exe window, and type the following commands to build the installer

cd c:\windows\ansmtpserver
mvn clean install

Open a windows explorer on C:\Windows\ansmtpserver\target

Right click the ansmtpserver-0.0.1-SNAPSHOT-installer.zip file and select “Extract all…” to the folder “C:\workspace\ansmtpserver\target”

Open the folder “C:\workspace\ansmtpserver\target\ansmtpserver-0.0.1-SNAPSHOT-installer”, right-click the “install.exe” file and select “Run as administrator”

Attach the zip file containing the installer to the maven artifact, so that the installer can be deployed to a maven repository, where other maven files can download and unpack the installer from (easy distribution)

Add the following inside <build><plugins></plugins></build> of the pom.xml build