进入tomcat下面的bin目录:
[root@tomcat ~]# cd /opt/tomcat/bin/
[root@tomcat bin]# tar xf commons-daemon-native.tar.gz
[root@tomcat bin]# cd commons-daemon-1.0.15-native-src/unix/
[root@tomcat unix]# ./configure --with-java=$JAVA_HOME
...
*** All done ***
Now you can issue "make"
[root@tomcat unix]# make
编译完成后,会在当前文件夹生成一个jsvc的文件,将它拷贝到/opt/tomcat/bin/下:
tomcat目录可自行设置
[root@tomcat unix]# cp jsvc /opt/tomcat/bin/
安装jsvc(无tomcat方式,利用commons-daemon-native.tar.gz和commons-daemon-1.1.0.jar)
将commons-daemon-native.tar.gz放到某个目录,这里放到/opt/jsvc
[root@localhost jsvc]# cd /opt/jsvc
[root@localhost jsvc]# ll
total 404
-rw-r--r--. 1 root root 25145 Aug 15 23:03 commons-daemon-1.1.0.jar
drwxr-xr-x. 4 root root 109 Aug 15 23:03 commons-daemon-1.1.0-native-src
-rw-r--r--. 1 root root 207125 Aug 15 23:03 commons-daemon-1.1.0-native-src.tar.gz
-rwxr-xr-x. 1 root root 174312 Aug 15 23:05 jsvc
解压commons-daemon-1.1.0-native-src.tar.gz
[root@localhost jsvc]# tar -xvf commons-daemon-1.1.0-native-src.tar.gz
commons-daemon-1.1.0-native-src/LICENSE.txt
commons-daemon-1.1.0-native-src/RELEASE-NOTES.txt
commons-daemon-1.1.0-native-src/NOTICE.txt
commons-daemon-1.1.0-native-src/README
commons-daemon-1.1.0-native-src/unix/
commons-daemon-1.1.0-native-src/unix/man/
commons-daemon-1.1.0-native-src/unix/native/
commons-daemon-1.1.0-native-src/unix/support/
commons-daemon-1.1.0-native-src/unix/configure.in
commons-daemon-1.1.0-native-src/unix/INSTALL.txt
commons-daemon-1.1.0-native-src/unix/man/jsvc.1.xml
commons-daemon-1.1.0-native-src/unix/man/README
commons-daemon-1.1.0-native-src/unix/native/debug.c
commons-daemon-1.1.0-native-src/unix/native/.indent.pro
commons-daemon-1.1.0-native-src/unix/native/locks.h
commons-daemon-1.1.0-native-src/unix/native/help.c
commons-daemon-1.1.0-native-src/unix/native/replace.c
commons-daemon-1.1.0-native-src/unix/native/java.c
commons-daemon-1.1.0-native-src/unix/native/replace.h
commons-daemon-1.1.0-native-src/unix/native/help.h
commons-daemon-1.1.0-native-src/unix/native/signals.c
commons-daemon-1.1.0-native-src/unix/native/jsvc-unix.c
commons-daemon-1.1.0-native-src/unix/native/location.h
commons-daemon-1.1.0-native-src/unix/native/locks.c
commons-daemon-1.1.0-native-src/unix/native/home.h
commons-daemon-1.1.0-native-src/unix/native/debug.h
commons-daemon-1.1.0-native-src/unix/native/java.h
commons-daemon-1.1.0-native-src/unix/native/version.h
commons-daemon-1.1.0-native-src/unix/native/arguments.h
commons-daemon-1.1.0-native-src/unix/native/dso.h
commons-daemon-1.1.0-native-src/unix/native/home.c
commons-daemon-1.1.0-native-src/unix/native/Makefile.in
commons-daemon-1.1.0-native-src/unix/native/location.c
commons-daemon-1.1.0-native-src/unix/native/signals.h
commons-daemon-1.1.0-native-src/unix/native/dso-dlfcn.c
commons-daemon-1.1.0-native-src/unix/native/dso-dyld.c
commons-daemon-1.1.0-native-src/unix/native/jsvc.h
commons-daemon-1.1.0-native-src/unix/native/arguments.c
commons-daemon-1.1.0-native-src/unix/Makedefs.in
commons-daemon-1.1.0-native-src/unix/support/config.guess
commons-daemon-1.1.0-native-src/unix/support/apsupport.m4
commons-daemon-1.1.0-native-src/unix/support/config.sub
commons-daemon-1.1.0-native-src/unix/support/apfunctions.m4
commons-daemon-1.1.0-native-src/unix/support/apjava.m4
commons-daemon-1.1.0-native-src/unix/Makefile.in
commons-daemon-1.1.0-native-src/unix/configure
commons-daemon-1.1.0-native-src/unix/man/fetch.sh
commons-daemon-1.1.0-native-src/unix/support/mkdist.sh
commons-daemon-1.1.0-native-src/unix/support/install.sh
commons-daemon-1.1.0-native-src/unix/support/buildconf.sh
commons-daemon-1.1.0-native-src/windows/
commons-daemon-1.1.0-native-src/windows/apps/
commons-daemon-1.1.0-native-src/windows/apps/prunmgr/
commons-daemon-1.1.0-native-src/windows/apps/prunsrv/
commons-daemon-1.1.0-native-src/windows/include/
commons-daemon-1.1.0-native-src/windows/resources/
commons-daemon-1.1.0-native-src/windows/xdocs/
commons-daemon-1.1.0-native-src/windows/src/
commons-daemon-1.1.0-native-src/windows/apps/prunmgr/prunmgr.c
commons-daemon-1.1.0-native-src/windows/apps/prunmgr/Makefile
commons-daemon-1.1.0-native-src/windows/apps/prunmgr/prunmgr.manifest
commons-daemon-1.1.0-native-src/windows/apps/prunmgr/prunmgr.h
commons-daemon-1.1.0-native-src/windows/apps/prunsrv/prunsrv.manifest
commons-daemon-1.1.0-native-src/windows/apps/prunsrv/Makefile
commons-daemon-1.1.0-native-src/windows/apps/prunsrv/prunsrv.c
commons-daemon-1.1.0-native-src/windows/apps/prunsrv/prunsrv.h
commons-daemon-1.1.0-native-src/windows/include/console.h
commons-daemon-1.1.0-native-src/windows/include/cmdline.h
commons-daemon-1.1.0-native-src/windows/include/apxwin.h
commons-daemon-1.1.0-native-src/windows/include/Makefile.inc
commons-daemon-1.1.0-native-src/windows/include/javajni.h
commons-daemon-1.1.0-native-src/windows/include/gui.h
commons-daemon-1.1.0-native-src/windows/include/handles.h
commons-daemon-1.1.0-native-src/windows/include/service.h
commons-daemon-1.1.0-native-src/windows/include/registry.h
commons-daemon-1.1.0-native-src/windows/include/log.h
commons-daemon-1.1.0-native-src/windows/include/rprocess.h
commons-daemon-1.1.0-native-src/windows/resources/license.rtf
commons-daemon-1.1.0-native-src/windows/xdocs/index.xml
commons-daemon-1.1.0-native-src/windows/src/javajni.c
commons-daemon-1.1.0-native-src/windows/src/mclib.h
commons-daemon-1.1.0-native-src/windows/src/console.c
commons-daemon-1.1.0-native-src/windows/src/service.c
commons-daemon-1.1.0-native-src/windows/src/handles.c
commons-daemon-1.1.0-native-src/windows/src/utils.c
commons-daemon-1.1.0-native-src/windows/src/registry.c
commons-daemon-1.1.0-native-src/windows/src/mclib.c
commons-daemon-1.1.0-native-src/windows/src/private.h
commons-daemon-1.1.0-native-src/windows/src/cmdline.c
commons-daemon-1.1.0-native-src/windows/src/gui.c
commons-daemon-1.1.0-native-src/windows/src/rprocess.c
commons-daemon-1.1.0-native-src/windows/src/log.c
commons-daemon-1.1.0-native-src/windows/README
commons-daemon-1.1.0-native-src/windows/apps/prunmgr/prunmgr.rc
commons-daemon-1.1.0-native-src/windows/apps/prunsrv/prunsrv.rc
commons-daemon-1.1.0-native-src/windows/resources/susers.bmp
commons-daemon-1.1.0-native-src/windows/resources/commons.bmp
commons-daemon-1.1.0-native-src/windows/resources/procruns.ico
commons-daemon-1.1.0-native-src/windows/resources/procrunr.ico
commons-daemon-1.1.0-native-src/windows/resources/procrunw.ico
[root@localhost jsvc]# ls
commons-daemon-1.1.0.jar commons-daemon-1.1.0-native-src commons-daemon-1.1.0-native-src.tar.gz
进入commons-daemon-1.1.0-native-src/unix目录
[root@localhost jsvc]# cd *src
[root@localhost commons-daemon-1.1.0-native-src]# ls
LICENSE.txt NOTICE.txt README RELEASE-NOTES.txt unix windows
[root@localhost commons-daemon-1.1.0-native-src]# cd unix
[root@localhost unix]# ls
configure configure.in INSTALL.txt Makedefs.in Makefile.in man native support
查找当前环境java_home目录
[root@localhost unix]# find / -name java
/etc/pki/ca-trust/extracted/java
/etc/pki/java
/etc/alternatives/java
/var/lib/alternatives/java
/usr/bin/java
/usr/local/zookeeper-3.4.12/contrib/loggraph/src/java
/usr/local/zookeeper-3.4.12/contrib/fatjar/src/java
/usr/local/zookeeper-3.4.12/contrib/ZooInspector/src/java
/usr/local/zookeeper-3.4.12/recipes/election/src/java
/usr/local/zookeeper-3.4.12/recipes/queue/src/java
/usr/local/zookeeper-3.4.12/recipes/lock/src/java
/usr/local/zookeeper-3.4.12/src/contrib/rest/src/java
/usr/local/zookeeper-3.4.12/src/contrib/loggraph/src/java
/usr/local/zookeeper-3.4.12/src/contrib/zooinspector/src/java
/usr/local/zookeeper-3.4.12/src/contrib/fatjar/src/java
/usr/local/zookeeper-3.4.12/src/recipes/election/src/java
/usr/local/zookeeper-3.4.12/src/recipes/queue/src/java
/usr/local/zookeeper-3.4.12/src/recipes/lock/src/java
/usr/local/zookeeper-3.4.12/src/java
/usr/java
/usr/java/jdk1.8.0_171-amd64/bin/java
/usr/java/jdk1.8.0_171-amd64/jre/bin/java
检查java环境是否正确
[root@localhost unix]# ./configure --with-java=/usr/java/jdk1.8.0_171-amd64
*** Current host ***
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking cached host system type... ok
*** C-Language compilation tools ***
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking for ranlib... ranlib
checking for strip... strip
*** Host support ***
checking C flags dependant on host system type... ok
*** Java compilation tools ***
checking JAVA_HOME... /usr/java/jdk1.8.0_171-amd64
checking for JDK os include directory... linux
gcc flags added
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... yes
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking sys/capability.h usability... no
checking sys/capability.h presence... no
checking for sys/capability.h... no
configure: WARNING: cannot find headers for libcap
*** Writing output files ***
configure: creating ./config.status
config.status: creating Makefile
config.status: creating Makedefs
config.status: creating native/Makefile
*** All done ***
Now you can issue "make"
输入make命令进行编译
[root@localhost unix]# make
(cd nativemake all)
make[1]: Entering directory `/opt/jsvc/commons-daemon-1.1.0-native-src/unix/native'
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c jsvc-unix.c -o jsvc-unix.o
jsvc-unix.c: In function ‘run_controller’:
jsvc-unix.c:1304:20: warning: assignment from incompatible pointer type [enabled by default]
act.sa_handler = controller
^
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c arguments.c -o arguments.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c debug.c -o debug.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c dso-dlfcn.c -o dso-dlfcn.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c dso-dyld.c -o dso-dyld.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c help.c -o help.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c home.c -o home.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c java.c -o java.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c location.c -o location.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c replace.c -o replace.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c locks.c -o locks.o
gcc -g -O2 -DOS_LINUX -DDSO_DLFCN -DCPU=\"amd64\" -Wall -Wstrict-prototypes -I/usr/java/jdk1.8.0_171-amd64/include -I/usr/java/jdk1.8.0_171-amd64/include/linux -c signals.c -o signals.o
ar cr libservice.a arguments.o debug.o dso-dlfcn.o dso-dyld.o help.o home.o java.o location.o replace.o locks.o signals.o
ranlib libservice.a
gcc jsvc-unix.o libservice.a -ldl -lpthread -o ../jsvc
make[1]: Leaving directory `/opt/jsvc/commons-daemon-1.1.0-native-src/unix/native'
编译完成,查看目录会生成jsvc文件,将jsvc拷贝到上级目录
[root@localhost unix]# ls
config.log config.nice config.status configure configure.in INSTALL.txt jsvc Makedefs Makedefs.in Makefile Makefile.in man native support
[root@localhost unix]# cp jsvc ../../
用来启动tomcat,在linux下面使用在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目录下
4.编写启动服务的脚本 myjsvc
#!/bin/sh
# myjsvc This shell script takes care of starting and stopping
#
# chkconfig: - 60 50
# description: tlstat stat is a stat data daemon.
# processname: myjsvc
# Source function library.
. /etc/rc.d/init.d/functions
RETVAL=0
prog="MYJSVC"
# jdk的安装目录
JAVA_HOME=/usr/java/jdk1.5.0_15
#应用程序的目录
MYJSVC_HOME=/test
#jsvc所在的目录
DAEMON_HOME=/usr/local/tomcat5/bin/jsvc-src
#用户
MYJSVC_USER=root
# for multi instances adapt those lines.
TMP_DIR=/var/tmp
PID_FILE=/var/run/tlstat.pid
#程序运行是所需的jar包,commons-daemon.jar是不能少的
CLASSPATH=/test/testjsvc.jar:/usr/local/tomcat5/bin/commons-daemon.jar:
case "$1" in
start)
#
# Start TlStat Data Serivce
#
$DAEMON_HOME/jsvc -user $MYJSVC_USER -home $JAVA_HOME -Djava.io.tmpdir=$TMP_DIR -wait 10 -pidfile $PID_FILE #控制台的输出会写到tlstat.out文件里
-outfile $MYJSVC_HOME/log/myjsvc.out -errfile '&1' -cp $CLASSPATH #服务启动类
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.TestJsvc
exit $?
*)
echo "Usage myjsvc start/stop"
exit 1
esac
5. 把myjsvc文件拷贝到/etc/init.d/目录下
6. #chmod -c 777 /etc/init.d/myjsvc
7. 添加服务
#chkconfig --add myjsvc
#chkconfig --level 345 myjsvc on
8. 完成,启动服务
#service myjsvc start
你可以从/test/log/myjsvc.out文件里看到如下信息:
execute init(args) method
execute start method
#service myjsvc stop
你会发现/test/log/myjsvc.out文件里会增加如下信息
execute stop method
execute destroy method
并且在系统重启时会自动启动myjsvc服务
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)