Deploying JAVA programs in Linux

Source: Internet
Author: User

After the JAVA program is developed, it needs to be deployed to the server. If it is a WEB project, it needs to be deployed to the WEB server; otherwise, it will be deployed to the application server.

JAVA is a cross-platform programming language. The operating system of the server can be Windows, Linux, or other programming languages. The Redhat6 operating system is used below,

Deploy JAVA programs on WEB servers and application servers in detail.

1. the JAVA program is deployed on the application server.

(1) directory structure deployed on Redhat6 by JAVA program HelloWorld

Bin: stores the shell script run. sh.

Conf: stores the configuration file log4j. properties.

Lib: store JAR package HelloWorld. jar, log4j-1.2.16.jar

Logs: stores the program running log File log. log

 

(2) Compile the test class HelloWorld. java and compress it into the JAR package 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. the value of the properties variable is in the script bin/run. sh read 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 ("print a log every 5 seconds");} catch (InterruptedException e) {e. printStackTrace (); log. error (e. getMessage () ;}}}; thread. run ();} catch (Exception e) {log. error ("[X] failed to start:" + e. getMessage (); System. exit (1 );}}}

(2) Compile the shell STARTUP script run. sh.

#! /Bin/sh # define variable # define APP_NAME = HelloWorldGREP_KEY = "Diname =" $ {APP_NAME} #-Xms512m set the initial memory of the JVM heap #-Xmx1024m set the maximum JVM heap memory #-Dlog4j. properties sets the log4j Log File parameters, which can be called by the JAVA program. The call format is System. getProperty ("log4j. properties ") APP_OPTS ="-Xrs-Xms512m-Xmx1024m-Dlog4j. properties = .. /conf/log4j. pr Operties "# main program class APP_CLASS =" com. test. helloWorld "# Log File APP_LOG = ".. /logs/log. log "# libAPP_LIBS =. /: 'ls .. /lib /*. jar | paste-s-d ":"-'# current class Path = Current module class path + JDK class path APP_CLASSPATH =$ {APP_LIBS }:.: $ {CLASSPATH} # Check whether the HelloWorld process is running. If it is running, 1 is returned. Otherwise, 0is_exist () {# ps-ef is returned: query all processes # grep-w "$ {GREP_KEY}": Find the process named HelloWorld from all processes.-w indicates exact search # grep-v "grep ": exclude the process # awk '{print $2}' whose name is grep: output the second parameter, that is, the process ID pid = 'ps -Ef | grep-w "$ {GREP_KEY}" | grep-v "grep" | awk '{print $2} ''# determine whether the process number is null if [-z "$ {pid}"] then return 1 else return 0fi} # print the status information of the HelloWorld process. status () {is_exist if [$? -Eq "0"] then echo "$ {APP_NAME} is running. pid =$ {pid }. "elseecho" $ {APP_NAME} is not running "fi} # start the HelloWorld process start () {is_exist if [$? -Eq "0"] then echo "$ {APP_NAME} is already running. pid =$ {pid }. "return 0 elseecho" try to start $ {APP_NAME }... "# Call the nohup command to start HelloWorld #1> &-: indicates that the standard output log is disabled to nohup. out #2 >$ {APP_LOG}: indicates that the log is output .. /logs/log. log # final &: indicates that the program does not exit nohup $ JAVA_HOME/bin/java-$ {GREP_KEY }$ {APP_OPTS}-classpath $ {APP_CLASSPATH }$ {APP_CLASS} 1> &- 2> $ {APP_LOG} & # It takes some time to start the program, the pause time (3 seconds) is set here. The unit is seconds 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 fifi} # stop the HelloWorld process stop () {is_existif [$? -Eq 0] then echo "try to stop $ {APP_NAME}..." # call the kill command to kill the process/usr/bin/kill-9 $ {pid} if [$? -Ne 0] then echo "failed to stop $ {APP_NAME }! "Return 1 elseecho" $ {APP_NAME} stopped. "return 0 fielseecho" $ {APP_NAME} is not running! "Return 1fi} # restart HelloWorld process restart () {stopstart} # display help information help () {echo" status show the status of $ {APP_NAME} server. "echo" start the $ {APP_NAME} server. "echo" stop the $ {APP_NAME} server. "echo" restart the $ {APP_NAME} server. "} # main function main () {case" $1 "in status) status; start) start; stop) stop; restart) restart ;;*) echo "command param error! See follow help "; help; esac} # execute the main function $1 to select the first string as the parameter. For example, the terminal command is :. /run. sh start status, select start as the input parameter main $1

(3) Compile the log configuration file log4j. properties

#stdoutlog4j.rootLogger=INFO, logfile#Filelog4j.appender.logfile=org.apache.log4j.DailyRollingFileAppenderlog4j.appender.logfile.File=../logs/log.loglog4j.appender.logfile.Append=truelog4j.appender.logfile.Threshold =INFO log4j.appender.logfile.DatePattern='.'yyyy-MM-ddlog4j.appender.logfile.layout=org.apache.log4j.PatternLayoutlog4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %5p %c{1}:%L - %m%n



(4) start the program

In the terminal directory/opt/HelloWorld/bin, enter the command:./run. sh start

View the content in the log file logs/log. log

Now, the JAVA program HelloWorld has been deployed on LINUX.

 

2. java web programs are deployed on the TOMCAT server.

(1) install JDK in Redhat

For more information, see my other blog.

Install JDK in Redhat

(2) install Tomcat in Redhat

For more information, see my other blog.

Install tomcat in Redhat

(3) JAVA Web program Packaging

For more information, see my other blog.

Use ant to create JAR and WAR packages

(4) deploying JAVA Web programs to Tomcat

1) store the WAR package of JAVA Web in tomcat/webapps

For example, my directory is/opt/apache-tomcat-6.0.37/webapps.

2) disable running tomcat

Enter the command on the terminal to check the process number that tomcat is running.

[Root @ bogon bin] # ps-ef | grep tomcat

Kill the tomcat process, where 2658 is the tomcat process number.

[Root @ bogon bin] # kill-9 2658

3) start tomcat

Go to the directory:/opt/apache-tomcat-6.0.37/bin, and enter the startup command

[Root @ bogon bin] #./startup. sh

View the startup log and go to the/opt/apache-tomcat-6.0.37/logs directory.

[Root @ bogon logs] # tail-20f catalina. out

If you see the log of successful startup:

Sep 27,201 3 10:35:55 org. apache. catalina. startup. Catalina start
INFO: Server startup in 1639 MS
Tomcat is started successfully.

4) Verification

Open your browser and enter URL: http: // localhost: 8080/manager/html

If you can see the deployed Java Web project, the deployment is successful.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.