Use Java service wrapper to use Java program as a Windows system service

Source: Internet
Author: User

From: http://www.cjsdn.net/post/view? Bid = 21 & id = 95116 & sty = 1 & TPG = 3 & age = 0

JavaProgramIn many cases, the program runs as a Service. On the UN * x platform, you can add "&" after the command to run the program as a background service, but in windows, if you think that console window is on the desktop, do you always worry about closing your console window at the same time? Do you miss Win32 service programs written in VC?
Open the JBoss and tomcat release packages and find that they all use an open source -- Java service wrapper. Using Java service Wrapper can easily solve our needs and make our service programs a Win32 system service.
Of course, Java service Wrapper can also be used in UN * X to avoid the brute force "&" method, resulting in receiving a bunch of emails every day, you can view running information in the log mode provided by Java service wrapper.
Java service wrapper is very powerful and supports both Windows and UN * X platforms. It provides three methods to package your Java program into a system service. Here is only the simplest method, because this method does not need to make any changes to the existing service programs, you can simply add a few scripts and configuration files to transform your Java service programs into system service programs.
Of course you need to http://sourceforge.net/project/showfiles.php before using? Group_id = 39428 download the Java service wrapper release package.

The following describes the specific steps:
1. decompress the downloaded Java service wrapper package to a local directory of {wrapper_home };
2. the Service Application name is myservapp. In the Directory D: \ myservapp, create the bin, Conf, logs, and Lib directories, and put your existing applications such as nioblockingserver. copy the class to this directory;
3. Copy the following files under {wrapper_home} \ SRC \ bin \ To The myservapp directory and rename them.
{Wrapper_home} \ bin \ wrapper.exe c: \ myservapp \ bin \ wrapper.exe
{Wrapper_home} \ SRC \ bin \ app. bat. In c: \ myservapp \ bin \ MyApp. bat
{Wrapper_home} \ SRC \ bin \ InstallApp-NT.bat.in c: \ myservapp \ bin \ InstallMyApp-NT.bat
{Wrapper_home} \ SRC \ bin \ UninstallApp-NT.bat.in c: \ myservapp \ bin \ UninstallMyApp-NT.bat
4. Copy the following files under {wrapper_home} \ Lib to the C: \ myservapp \ lib directory.
{Wrapper_home} \ Lib \ wrapper. dll
{Wrapper_home} \ Lib \ wrapper. Jar
5. set {wrapper_home} \ SRC \ conf \ wrapper. conf. copy in to the C: \ myservapp \ conf directory and name it wrapper. conf; and modify wrapper. CONF file to configure your application service.
Modify the following items:
# Your JVM location:
Wrapper. java. Command = D: \ sun \ j2sdk1.4.0 _ 03 \ bin \ Java
# Running Parameters: for example:
Wrapper. java. additional.1 =-dprogram. Name = run. bat
# Classpath:
Wrapper. java. classpath.1 = ../lib/wrapper. Jar
Wrapper. java. classpath.2 =.../bin /.
# Java library path (location of wrapper. dll or libwrapper. So)
Wrapper. java. Library. path.1 = ../lib
# The main class determines how to use Java service wrapper.
Wrapper. java. mainclass = org. tanukisoftware. wrapper. wrappersimpleapp
# Your Java application class
Wrapper. App. parameter.1 = nonblockingserver
# Service name
Wrapper. ntservice. Name = Nb

# Display name of the service
Wrapper. ntservice. displayname = NiO nonblocking Server
# Service description
Wrapper. ntservice. Description = NiO nonblocking Server
Other configurations can be changed as needed.
6. Test the MyApp. Bat configured above and run MyApp. bat, just like running tomcat in the console window;
7. Test the service configured above. Running c: \ myservapp \ bin \ InstallMyApp-NT.bat will install your application (nioblockingserver here) into the Win32 system service.
8. Open the control panel-administrative programs-service, and you will see that NiO nonblocking server is already in the system service. Other usage is the same as the Windows service we are familiar.

Tomcat uses the Java service wrapper Mode 2, which requires minor changes to existing programs. However, you can use the socket port to control the startup of the service program core, which is more flexible. Java service wrapper provides Mode 3 that is complex and requires more coding. I have not studied it.
Using Mode 1, we can simply and effectively package our service programs into system service programs, and enhance the log function. We can make several myservapp files into templates and modify the file names each time, the configuration file can be used. If you have the energy, you can make the Eclipse plug-in. With a few clicks, you can configure the application as a service.

The attachment is a template. You can directly modify the file name and configuration file to transform the service.
I hope this article will be helpful to you, and of course I have applied for extra points. I am not mixed enough.

/Files/cai9911/ntservicewrappertemplate.rar

================================
You can directly modify an example by yourself:
(Write down the memo)

Modify config/wrapper. conf:
Wrapper. App. parameter.1 = helloworld <class to be started>

You can modify the service name of an application.

/Files/cai9911/assumervice.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.