JAVA程序在開發完成後,須要部署到服務器,若是是WEB項目,須要部署到WEB服務器,不然部署到應用服務器。java
JAVA是跨平臺的編程語言,服務器的操做系統能夠是Windows、Linux或者其它,下面將在Redhat6操做系統下,shell
詳細說明JAVA程序在WEB服務器和應用服務器上的部署狀況。apache
一、JAVA程序部署在應用服務器編程
(1) JAVA程序HelloWorld 在Redhat6上部署的目錄結構服務器
bin : 存放shell腳本run.sh編程語言
conf :存放配置文件log4j.properties函數
lib :存放JAR包HelloWorld.jar、log4j-1.2.16.jar測試
logs:存放程序運行日誌文件log.logspa
(2)編寫測試類HelloWorld.java 並打成JAR包HelloWorld.jar操作系統
package com.test; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; public class HelloWorld { private static Logger log = Logger.getLogger(HelloWorld.class); public static void main(String[] args) { try{ //log4j.properties變量的值在腳本bin/run.sh 中讀取 String config = System.getProperty("log4j.properties"); if (config != null) { PropertyConfigurator.configure(config); } log.info("HelloWorld"); Thread thread = new Thread(){ public void run(){ while(true){ try { Thread.sleep(5*1000); log.info("每隔5秒打印一下日誌"); } catch (InterruptedException e) { e.printStackTrace(); log.error(e.getMessage()); } } } }; thread.run(); } catch (Exception e) { log.error("[X]啓動失敗:"+e.getMessage()); System.exit(1); } } }
(2)編寫shell啓動腳本run.sh
#! /bin/sh #------------------------------------------------------------------- # 定義變量 #------------------------------------------------------------------- APP_NAME=HelloWorld GREP_KEY="Diname="${APP_NAME} # -Xms512m 設置JVM堆的初始內存 # -Xmx1024m 設置JVM堆的最大內存 # -Dlog4j.properties 設置log4j日誌文件參數,可給JAVA程序調用,調用格式是System.getProperty("log4j.properties") APP_OPTS="-Xrs -Xms512m -Xmx1024m -Dlog4j.properties=../conf/log4j.properties" # 程序主類 APP_CLASS="com.test.HelloWorld" # 日誌文件 APP_LOG="../logs/log.log" # 模塊運行須要的lib APP_LIBS=./:`ls ../lib/*.jar | paste -s -d":" -` # 當前的類路徑=當前模塊的類路徑+JDK的類路徑 APP_CLASSPATH=${APP_LIBS}:.:${CLASSPATH} # 檢查HelloWorld進程是否已經在運行,若是在運行則返回1,不然返回0 is_exist(){ # ps -ef : 查詢全部進程 # grep -w "${GREP_KEY}" : 從全部進程中查出名稱爲HelloWorld的進程,-w爲精確查找 # grep -v "grep" : 排除名稱爲grep的進程 # awk '{print $2}' : 輸出第二個參數,也就是進程號 pid=`ps -ef | grep -w "${GREP_KEY}" | grep -v "grep" | awk '{print $2}'` # 判斷進程號是否爲空 if [ -z "${pid}" ] then return 1 else return 0 fi } # 打印HelloWorld進程的狀態信息 status(){ is_exist if [ $? -eq "0" ] then echo "${APP_NAME} is running. pid=${pid} ." else echo "${APP_NAME} is not running" fi } # 啓動HelloWorld進程 start(){ is_exist if [ $? -eq "0" ] then echo "${APP_NAME} is already running. pid=${pid} ." return 0 else echo "try to start ${APP_NAME} ... " # 調用nohup命令啓動HelloWorld # 1>&- : 表示關閉標準輸出日誌到nohup.out # 2>${APP_LOG} : 表示輸出日誌到../logs/log.log # 最後的& : 表示退出賬戶/關閉終端時程序不退出 nohup $JAVA_HOME/bin/java -${GREP_KEY} ${APP_OPTS} -classpath ${APP_CLASSPATH} ${APP_CLASS} 1>&- 2>${APP_LOG} & # 程序的啓動須要必定的時間,這裏設置暫停時間(3秒),單位是秒 sleep 3 is_exist if [ $? -eq "0" ] then echo "${APP_NAME} is running now. pid=${pid}." return 0 else echo "failed to start ${APP_NAME}! see ${APP_LOG} for more details." return 1 fi fi } # 中止HelloWorld進程 stop() { is_exist if [ $? -eq 0 ] then echo "try to stop ${APP_NAME} ..." # 調用kill命令殺掉進程 /usr/bin/kill -9 ${pid} if [ $? -ne 0 ] then echo "failed to stop ${APP_NAME}!" return 1 else echo "${APP_NAME} stopped." return 0 fi else echo "${APP_NAME} is not running!" return 1 fi } # 重啓HelloWorld進程 restart(){ stop start } # 顯示幫助信息 help() { echo "status show the status of ${APP_NAME} server." echo "start start the ${APP_NAME} server." echo "stop stop the ${APP_NAME} server." echo "restart restart the ${APP_NAME} server." } # 主函數 main() { case "$1" in status) status;; start) start;; stop) stop;; restart) restart;; *) echo "command param error ! see follow help "; help;; esac } # 執行主函數 $1表示選擇第一個字符串爲參數,好比終端命令是:./run.sh start status,則選擇start爲輸入參數 main $1
(3)啓動程序
在終端目錄/opt/HelloWorld/bin下,輸入命令:./run.sh start
查看日誌文件logs/log.log中的內容
至此,JAVA程序HelloWorld已經在LINUX上部署完成。
二、JAVA WEB程序部署在TOMCAT服務器
待續...