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 |
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 |
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 |
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 |
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:
- The demo program is a simple ftp server. ( Source code)
- Compile and pack into a jar file: jftpsvr.jar
- Start Jar2Exe, at step 1, click Browse Jar and select 'jftpsvr.jar'
- At step 1, click the Min box and edit it to 1.4.2.
- At step 2, select Service application.
- At step 3, select the main class com.regexlab.ftp.FtpServer
- At step 3, input a NT service name: jftpsvr
- At step 4, check the Encrypt and hide class files option.
-
At step 7(Finished), click Config External, then
- At Custom tab, append "stdout |cronolog.exe %Y%m%d.log" to redirect System.out to a daily log file.
- At Custom tab, append "stderr |cronolog.exe %Y%m%d.log" to redirect System.err to a daily log file.
- Click OK to close this popup dialog
- Download cronolog from http://cronolog.org , put the "cronolog.exe" along with jftpsvr.exe
Download
- You can download the demo program and get more usage on this page.
- solutions-all.zip - 11.3M, all solution related files in one file.
Comments
Allow changing service name during installation
As far as I know service name for Windows service could be defined only during build process. We tremendously need to install the same executable as multiple services with different names. Is there any possibility to do this e.g. via command line parameter:
myservice.exe /install /service MyServiceName
thanks for response
Not provided by Jar2Exe
It is possible in technique, but Jar2Exe does not provide this feature. The service name is fixed when generating exe.
Application dosen't stand after program is over
Dear sir,
I have created a application on displaying tables of numbers but the application gets close as soon as tables are printed.
please help.
Thank you.
Add new comment