Solution Advice: Service

A service program runs in background continuously


Introduction

If your java program performs a service, for example, listen on certain tcp port or do a background task, you can run your java program as a service.


Solution

To run as a service, your java program does not need any special coding, but just keep running by 'accept()' or 'sleep()' in your java program as normal. After the service program is installed, it can automatically start up when System starts up. Service program usually runs in background for a long time, you can redirect System.out and System.err to a disk file as a log file.


Features used

Following features are advised to use:

Feature Advice Introduction How to enable

Service application

Required

To run as a background service, it is required to create a service type of application.

At Step 2, select Service application

Integrate develop

Optional

If you need to get the Event of stop service, you can call integrate API to set a listener.

Set a listener to integrate API

Minimum JRE
version

Recommended

If you use a feature since certain version of JRE, you'd better set a min JRE version.

At Step 1, select a Min version

Encryption

Recommended

If you want to protect your program from being extracted and decompiled, you can select to encrypt it.

At Step 4, check the encrypt option

Event log

Recommended

To record event to System event log, you can call the event log API.

At Step 4, check the event log

Force CWD

Enabled by default

Disk IO with 'relative path' are based from the "working directory". By default, the working directory of service program is set to the directory where the exe file is.

At Step 6 > Config Internal > Custom tab, use a 'forcecwd' item

Stdout redirect

Enabled by default

Because the console window is not visible, if you need to get the content of System.out, you can redirect it  to a disk file. By default, the System.err of service program is redirect to a disk file.

At Step 6 > Config Internal > Custom tab, use a 'stdout' item

Stdout redirect
to cronolog

Optional

You can redirect stdout to a pipe, "| cronolog.exe %Y%m%d.log", to split output to a daily log file.

Use a "stdout | cronolog.exe" in config

Program icon

Optional

Service type of application is started background, so a program icon is not necessary.

At Step 6 > Version & messages


Steps

Steps to create service program:

  1. The demo program is a simple ftp server. ( Source code)
  2. Compile and pack into a jar file: jftpsvr.jar
  3. Start Jar2Exe, at step 1, click Browse Jar and select 'jftpsvr.jar'
  4. At step 1, click the Min box and edit it to 1.4.2.
  5. At step 2, select Service application.
  6. At step 3, select the main class com.regexlab.ftp.FtpServer
  7. At step 3, input a NT service name: jftpsvr
  8. At step 4, check the Encrypt and hide class files option.
  9. At step 7(Finished), click Config External, then
    1. At Custom tab, append "stdout |cronolog.exe %Y%m%d.log" to redirect System.out to a daily log file.
    2. At Custom tab, append "stderr |cronolog.exe %Y%m%d.log" to redirect System.err to a daily log file.
    3. Click OK to close this popup dialog
  10. Download cronolog from http://cronolog.org , put the "cronolog.exe" along with jftpsvr.exe

Download

Add new comment