Article Title: deploy a Java program on Linux and make it run regularly. Linux is a technology channel of the IT lab in China. Including desktop applications, Linux system management, kernel research, embedded systems and open source, and other basic categories I. Environment Introduction:
SecureCRT5.1 + winXP + WMware + Redhat
NOTE: If SecureCRT cannot connect to linux on the virtual machine, you should pay attention to two places. One is whether the NIC on the virtual machine is bridging or directly connected to the physical Nic, whether the ip address is in the same network segment as the ip address of the xp Server. If linux on a virtual machine can access the Internet, but it cannot be connected, enter services iptables stop on the linux terminal.
2. Install jdk on linux
Go to the java official website next suitable for linux jdk, we next jdk-6u4-linux-i586.bin here, use SecureCRT to connect to linux, enter the directory we will put jdk, enter rz, in the pop-up dialog box, select jdk next to us to upload it to linux. Use the ls command to check whether the file is uploaded, and then add executable permissions to the file: chmod + x jdk-6u4-linux-i586.bin, and then execute the file :. Jdk-6u4-linux-i586.bin then you will see the jdk installation information. If you want to enter yes or no, enter yes. after the installation is complete, configure the java environment variable, modify the/etc/profile file: vi/etc/proifle, press the letter a to enter the insertion mode, and add the following lines at the end:
PATH=$PATH:/usr/java/jdk1.6/binexport PATHJAVA_HOME=/usr/java/jdk1.6export JAVA_HOME
|
Press esc and enter wq to save the file. Enter java-version on the terminal and press Enter. If the following information appears, it indicates that jdk has been installed successfully.
java version "1.6.0_04"Java(TM) SE Runtime Environment (build 1.6.0_04-b12)Java HotSpot(TM) Client VM (build 10.0-b19, mixed mode, sharing)
|
3. Write a java program to recognize its log:
Create a project using eclipse: The linuxtest package structure is as follows:
--linuxtest/src/com/dao/Appdao.java--linuxtest/src/com/main/Main.java--linuxtest/src/log4j.properties
|
The content of these three files is as follows:
Appdao:java:package com.dao;import org.apache.log4j.Logger;public class Appdao {private static final Logger logger=Logger.getLogger(Appdao.class);public void show(){logger.info("this is info message");logger.debug("this is debug message");logger.error("this is error message");logger.fatal("this is fatal message");}}Main.java:package com.main;import com.dao.Appdao;public class Main {public static void main(String[] args) {Appdao dao=new Appdao();dao.show();}}log4j.propertieslog4j.rootLogger=DEBUG,CONSOLE log4j.addivity.org.apache=true log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=DEBUG log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n #log4j.appender.CONSOLE.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD] n%c[CATEGORY]%n%m[MESSAGE]%n%n
|
Add required jar package: commons-logging-1.1.1.jar log4j-1.2.8.jar
4. Package the program into a jar package
Use java jar commands or other packaging commands to compress the program into a jar package and test whether java-jar can be successfully executed in winxp dos, after the execution is successful, upload it to linux. For more information, see my other article. Package the program here and get the following file: linuxtest. jar and a lib folder, which contains the jar package required for running the program. For transmission convenience, I compress all the files into a zip package: app.zip uses the rz command to upload them to the/webapps/directory on linux.
5. Compile the script for executing the program
Cd/webapps directory
Rz uploads the app.zip file to this directory.
Unzip app.zip decompress the file
Rm app.zip Delete app.zip
Ls you can see the following content:
Lib linuxtest. jar
Cat> start. sh: Create a script file and enter the following content:
Java-jar linuxtest. jar
CTRL + D save the file
Add executable permissions to chmod + x start. sh
. Start. sh: Execute the script to run the program. If you see the following content, it indicates that you have succeeded:
[framework] 2008-02-24 19:44:19,181 - com.dao.Appdao -0 [main] INFO com.dao.Appdao - this is info message[framework] 2008-02-24 19:44:19,187 - com.dao.Appdao -6 [main] ERROR com.dao.Appdao - this is error message[framework] 2008-02-24 19:44:19,188 - com.dao.Appdao -7 [main] FATAL com.dao.Appdao - this is fatal message
|
6. Add this program to the scheduled task.
Crontab-l view existing scheduled tasks,
Crontab-e: Add or modify a scheduled task and add the following lines:
# Run/webapps/linuxtest. jar every minute
1 ***/webapps/linuxtest/start. sh
Then, Press esc and enter wq to save the file.
# Followed by comments
1 */webapps/linuxtest/start. the first six digits of sh, f1 indicates minute, f2 indicates hour, f3 indicates the day of a month, f4 indicates month, f5 indicates the day of a week, f6 indicates the script to be executed.