首页 > 代码库 > 【Linux_Java】Linux部署运行Java应用程序相关
【Linux_Java】Linux部署运行Java应用程序相关
以前Java程序部署都交给维护,最近一个项目要自己包办一切,发觉有许多小细节其实一直不是很清楚。其实无论问题多么的奇葩都是可以解决的,就是免不了折腾。
这里只记录几个比较基本和关键的东西。
1.linux安装jdk/jre;
1)安装:
建立jdk想要安装的目录,把文件拷贝进去
chmod a+x jre-6u45-linux-i586.bin
./jre-6u45-linux-i586.bin 2) 配置环境变量:
<1># vi /etc/profile
<2>在最后加入以下几行:
export JAVA_HOME=/usr/java/jre1.6.0_45
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
===
如果是jre,则是如下几行:
export JAVA_HOME=/usr/java/jre1.6.0_45
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin
<3># source /etc/profile 执行后生效
删除
rm -rf xxx 强制删除文件和文件夹
<2>在最后加入以下几行:
export JAVA_HOME=/usr/java/jre1.6.0_45
export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
===
如果是jre,则是如下几行:
export JAVA_HOME=/usr/java/jre1.6.0_45
export CLASSPATH=$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib
export PATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$PATH:$HOMR/bin
<3># source /etc/profile 执行后生效
2.编写启动Java应用的shell脚本;
一个比较通用的脚本例子,该一下相关配置即可:
#!/bin/bash
#=======================================================================
# Mutable Properties
#=======================================================================
PROC_TAG="middleware_service"
VM_FLAG="-Xrs -Xms300M -Xmx758M -Ddefault.client.encoding=UTF-8 -Dfile.encoding=UTF-8 -Duser.language=Zh"
MAIN_CLASS="launcher.Launcher"
LOADER_PATH="../lib/**/*.jar ../ lib/*.jar ../ conf"
java_home=$JAVA_HOME
#java_home=/user
LOG_FILE=../logs/console.out
#=======================================================================
# Define information tag
#=======================================================================
RUNNING_TAG="[R]"
NOT_RUNNING_TAG="[X]"
ERROR_TAG="[E]"
INFO_TAG="[I]"
#=======================================================================
# Define functions for process
#=======================================================================
set_classpath(){
set ${LOADER_PATH}
while [ $# - gt 0 ]; do
classpath=${ classpath}:$1
shift
done
CLASSPATH=${ classpath}:${CLASSPATH}
}
is_proc_run(){
ps - ef | grep -w "${PROC_TAG}" | grep -v grep &>/ dev/null
return $?
}
status_proc(){
is_proc_run
if [ $? - eq 0 ]; then
echo "${RUNNING_TAG} ${PROC_TAG} is running !"
ps - ef | grep -w "${PROC_TAG}" | grep -v grep
set_classpath
${java_home}/bin/java - Diname=${PROC_TAG} ${VM_FLAG} -cp ${CLASSPATH} ${MAIN_CLASS} status
else
echo "${NOT_RUNNING_TAG} ${PROC_TAG} is not running !"
fi
}
start_proc(){
is_proc_run
if [ $? - eq 0 ]; then
echo "${INFO_TAG} ${PROC_TAG} is already running !"
else
echo "${INFO_TAG} Starting ${PROC_TAG} ..."
set_classpath
nohup ${java_home}/bin/java - Diname=${PROC_TAG} ${VM_FLAG} -cp ${CLASSPATH} ${MAIN_CLASS} start > ${LOG_FILE} 2>&1 &
sleep 1
is_proc_run
if [ $? - eq 0 ]; then
echo "${INFO_TAG} ${PROC_TAG} started !"
else
echo "${ERROR_TAG} ${PROC_TAG} starts failed !"
fi
fi
}
stop_proc(){
set_classpath
${java_home}/bin/java - Diname=${PROC_TAG} ${VM_FLAG} - cp ${CLASSPATH} ${MAIN_CLASS} stop
sleep 1
is_proc_run
if [ $? - eq 0 ]; then
pid=` ps - ef| grep " Boce"| grep -v grep | awk ‘{print $2}‘`
kill ${ pid}
fi
}
usage(){
echo ${PROC_DESC} usage:
echo -e "` basename $0` <start|stop|status|restart>"
echo -e "\ tstart - start ${PROC_TAG}"
echo -e "\ tstop - stop ${PROC_TAG}"
echo -e "\ tstatus - list ${PROC_TAG}"
echo -e "\ trestart - restart ${PROC_TAG}"
}
#=======================================================================
# Main Program begin
#=======================================================================
case $1 in
start)
start_proc
;;
status)
status_proc $2 $3
;;
stop)
stop_proc
;;
restart)
stop_proc
start_proc
;;
*)
usage
esac
3.部署运行相关的有用命令;
解压
tar -xf all.tar删除
rm -rf xxx 强制删除文件和文件夹
更改权限:
chmod 777 xxx
运行:
./xxx.sh start
【Linux_Java】Linux部署运行Java应用程序相关
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。