Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to embed Java programs into Linux Services using Jsvc

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Jsvc to embed Java programs into Linux services". The explanation in this article is simple and clear and easy to learn and understand. please follow the editor's ideas to study and learn "how to use Jsvc to embed Java programs into Linux services".

Start the java program as a service on linux

1. Install jsvc

There is a jsvc.tar.gz file in the bin directory of tomcat. Go to the bin directory of tomcat.

# tar xvfz jsvc.tar.gz

# cd jsvc-src

# sh support/buildconf.sh

# chmod 755 configure

#. / configure-- with-java=/usr/local/java (change to your JDK location)

# make

two。 Write service startup classes

Package com.sohu.jsvc.test;public class TestJsvc {public static void main (String args []) {System.out.println ("execute main method!");} public void init () throws Exception {System.out.println ("execute init method!") Public void init (String [] args) throws Exception {System.out.println ("execute init (args) method");} public void start () throws Exception {System.out.println ("execute start method!") ;} public void stop () throws Exception {System.out.println ("execute stop method!") ;} public void destroy () throws Exception {System.out.println ("execute destroy method!");}}

The main method can be removed, but the init (String [] args), start (), stop (), destroy () methods are indispensable. When the service starts, the init (String [] args) method is called first.

Then call the start () method, first call the stop () method when the service is stopped, and then call the destroy () method.

3. Package this class into testjsvc.jar and put it in the / test directory

4. Write a script to start the service, myjsvc

#! / bin/sh# myjsvc This shell script takes care of starting and stopping## chkconfig:-60 5 minutes description: tlstat stat is a stat data daemon.# processname: myjsvc# Source function library.. / etc/rc.d/init.d/functionsRETVAL=0prog= "MYJSVC" # installation directory of jdk the directory where the JAVA_HOME=/usr/java/jdk1.5.0_15# application is located, the directory where MYJSVC_HOME=/test#jsvc is located, the DAEMON_HOME=/usr/local/tomcat5/bin/jsvc-src# user MYJSVC_USER=root# for multi instances adapt those lines.TMP_DIR=/var/tmpPID_FILE=/var/run/tlstat.pid# program is required to run the jar package Commons-daemon.jar is indispensable CLASSPATH=\ / test/testjsvc.jar:\ / usr/local/tomcat5/bin/commons-daemon.jar:\ case "$1" instart) # # Start TlStat Data Serivce#$DAEMON_HOME/jsvc\-user $MYJSVC_USER\-home $JAVA_HOME\-Djava.io.tmpdir=$TMP_DIR\-wait 10\-pidfile $PID_FILE\ # the output of the console will be written to the tlstat.out file-outfile $MYJSVC_HOME/log/myjsvc.out\-errfile'& 1' \-cp $CLASSPATH\ # Service startup class com.sohu.jsvc.test.TestJsvc # # To get a verbose JVM#-verbose\ # To get a debug of jsvc.#-debug\ exit $? ; stop) # # Stop TlStat Data Serivce#$DAEMON_HOME/jsvc\-stop\-pidfile $PID_FILE\ com.sohu.jsvc.test.TestJsvcexit $?;; *) echo "Usage myjsvc start/stop" exit 1 ESAC

5. Copy the myjsvc file to the / etc/init.d/ directory

6. # chmod-c 777 / etc/init.d/myjsvc

7. Add Servic

# chkconfig-add myjsvc

# chkconfig-- level 345 myjsvc on

8. Complete, start the service

# service myjsvc start

You can see the following information from the / test/log/myjsvc.out file:

Execute init (args) method

Execute start method

# service myjsvc stop

You will find the following information added to the / test/log/myjsvc.out file

Execute stop method

Execute destroy method

And the myjsvc service will be started automatically when the system is restarted

OK, just write a simple liunx service. You can add your business to the init (), start (), stop (), destroy () methods of TestJsvc and do what you want.

Thank you for reading, the above is the content of "how to use Jsvc to embed Java programs into Linux services". After the study of this article, I believe you have a deeper understanding of how to use Jsvc to embed Java programs into Linux services, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report