:: if you never try you never know ::

software development and best practice

Running Java Applications as a Linux Service


Overview

Sometimes application have the need to run java application as Linux Service. Aplicatioin needs to start, stop, restart easily. In traditional method, I usually run java application using command line
java -jar applicationame
when I need to restart the application, I will do:
- Find process ID of the application started before
- Kill process kill -9 PID
- start java apps, java -jar applicationname

Besides complicated, this method also has disadvantages such as the when start service for two times there is no prevention if the service running.

Java Service Wrapper

Using Java Service Wrapper, the complected steps above can be simplified.It also support Linux and Windows Platform.

The case study is scheduler will print log every one second. To know about quartz using spring support please check this article

First thing first is download java service wrapper library , I use 3.5.7 version for linux 64 bit. Unzip the file. If you want to use the java service wrapper to production you can remove docs, jdoc, src directory and readme files.
export the application to jar file, you can use you IDE to make it simple.

Manage Artifact

Go to wrapper_home,
copy application jar to wrapper_home/jsw.jar
copy application configuration file such: application.properties, application-context.xml to wrapper_home/
copy application library to wrapper_home/lib

Configuration

open wrapper configuration file wrapper_home/conf/wrapper.conf and change some configuration file:

Wrapper Class

wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp

Configure application jar file

wrapper.java.classpath.3=../jsw.jar

Configure application lib dependency

wrapper.java.classpath.5=../lib/commons-logging.jar
wrapper.java.classpath.6=../lib/org.springframework.asm.jar
wrapper.java.classpath.7=../lib/org.springframework.beans.jar
wrapper.java.classpath.8=../lib/org.springframework.context.jar
wrapper.java.classpath.9=../lib/org.springframework.context.support.jar
wrapper.java.classpath.10=../lib/org.springframework.core.jar
wrapper.java.classpath.11=../lib/org.springframework.expression.jar
wrapper.java.classpath.12=../lib/org.springframework.transaction.jar
wrapper.java.classpath.13=../lib/log4j-1.2.15.jar
wrapper.java.classpath.14=../lib/quartz-1.8.3.jar
wrapper.java.classpath.15=../lib/slf4j-api-1.5.10.jar
wrapper.java.classpath.16=../lib/slf4j-log4j12-1.5.10.jar

Configure application main class

wrapper.app.parameter.1=jsw.main.Main

Configure Service Description

# Name of the service
wrapper.name=jsw
# Display name of the service
wrapper.displayname=Java Service Wrapper Example
# Description of the service
wrapper.description=Java Service Wrapper Example Description

Go to wrapper_home/bin change testwrapper file name to jws to make it more explicit to service name.

Test Application

Go to wrapper_home/bin
install service: sh jws install
start service: sh jws start

To check the start service successfully executed, check log the log file wrapper_home/logs/wrapper.log

restart service: sh jws restart
stop service: sh jws stop

Download

download spring quartz scheduler source code.

About these ads

5 responses to “Running Java Applications as a Linux Service

  1. Peter February 9, 2012 at 12:52 pm

    What are the pricing details of it? Seems very expensive if you have to distribute as part of linux package of your hardware

  2. Pingback: Running Java as a service (daemon) on a Linux machine | Smash Company

  3. kink in my neck July 4, 2013 at 5:06 pm

    Hello! This is kind of off topic but I need some help from an established blog.
    Is it hard to set up your own blog? I’m not very techincal but I can figure things out pretty fast. I’m thinking about creating
    my own but I’m not sure where to start. Do you have any points or suggestions? Thank you

  4. whatsapp for windows July 11, 2013 at 11:10 pm

    I constantly spent my half an hour to read this webpage’s posts everyday along with a mug of coffee.

  5. candy crush cheats August 7, 2013 at 1:11 pm

    whoah this blog is wonderful i like studying your posts.
    Keep up the great work! You know, lots of individuals are hunting around for
    this info, you can help them greatly.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: