利用Jsvc把Java程序嵌入到Linux服务中去(1)


在linux上以服务的方式启动java程序

1.安装jsvc

在tomcat的bin目录下有一个jsvc.tar.gz的文件,进入tomcat的bin目录下

#tar xvfz jsvc.tar.gz

#cd jsvc-src

#sh support/buildconf.sh

#chmod 755 configure

#./configure --with-java=/usr/local/java (改成你的JDK的位置)

#make


2.编写服务启动类

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!");
}

}
main方法可以去掉,但是init(String[] args),start(),stop(),destroy()方法不能少,服务在启 动时会先调用init(String[] args)方法

然后调用start()方法,在服务停止是会首先调用stop()方法,然后调用destroy() 方法.

3.把这个类打包成testjsvc.jar 放到/test目录下


相关内容

    暂无相关文章