Shell script runs Java program

Source: Internet
Author: User
Tags log4j

When we deploy the project on Linux, we often start the program through a shell, or even call the Java program periodically via the Crontab timer task, but one of the strangest questions is that I wrote a shell script like this:

#!/bin/sh #-----------------------------------------------------------------------------# Start script for the CMGP Bosscontrol # # $Id: Run_bosscontrol.sh,v 1.0 2007/11/06 EXP $ #------------------------------------------------------- ----------------------#指定字符集 LANG=ZH_CN. GBK Export LANG run_home=. Classpath= $CLASSPATH: $RUN _home/lib/checking.jar classpath= $CLASSPATH: $RUN _home/lib/ojdbc14.jar classpath=$ CLASSPATH: $RUN _home/lib/commons-dbutils-1.1.jar classpath= $CLASSPATH: $RUN _home/lib/log4j-1.2.14.jar classpath=$ CLASSPATH: $RUN _home/lib/dom4j-1.6.jar export CLASSPATH java com.**.checking. Checking_start >> Log.out &

Manual command line to run the script, you can run the Java program, but the use of crontab timing task, seemingly does not work, is very depressing where to find out why, analysis possible reasons:

1 whether the current user does not have executable rights to this shell script, the ls-lrt/apps/service/mtk/checking/run.sh view script is executable, but has execute permission Ah-rwxr-xr-x

2 since running the script alone is no problem, it will be the problem of timing. So writing a simple output of the shell script through timing is no problem. Description or the problem with the script.

Later on the internet, think of the possible reasons for the environment variables in the script, because run the script through crontab, rather than the current user, so cat/etc/profile view the environment variables, and then modify the script as follows:

#!/bin/sh #-----------------------------------------------------------------------------# Start script for the CMGP Bosscontrol # # $Id: Run_bosscontrol.sh,v 1.0 2007/11/06 EXP $ #------------------------------------------------------- ----------------------Export path=/apps/usr/java/jdk1.5/bin: $PATH export java_home=/apps/usr/java/jdk1.5 export JRE _HOME=/APPS/USR/JAVA/JDK1.5/JRE Export classpath=/apps/usr/java/jdk1.5/lib:/apps/usr/java/jdk1.5/jre/lib:$ CLASSPATH run_home=/apps/service/checking classpath= $CLASSPATH $run_home/lib/checking.jar classpath= $CLASSPATH: $ Run_home/lib/ojdbc14.jar classpath= $CLASSPATH: $RUN _home/lib/commons-dbutils-1.1.jar classpath= $CLASSPATH: $RUN _ Home/lib/log4j-1.2.14.jar classpath= $CLASSPATH: $RUN _home/lib/dom4j-1.6.jar export classpath= $CLASSPATH java com.**. Checking. Checking_start >> Log.out &

Export shows environment variables exported as user environment variables
The above jar packages are exported through the Eclipse tool export and do not contain MANIFEST.MF files, and if you use the packaging tools ant, we can set the Class-path in the package default Build.xml file
Add a third party jar package to the MANIFEST.MF file and specify the program main class
Add the following content to the Build.xml:

<!--Create a property containing all. jar files, prefix lib/, and seperated with a space--> <pathconvert proper Ty= "Libs.project" pathsep= "" > <mapper> <chainedmapper> <!--remove absolute path--> < Flattenmapper/> <!--add lib/prefix--> <globmapper from= "*" to= "lib/*"/> </chainedmapper> </ma Pper> <path> <!--lib.home contains all jar files, several subdirectories--> <fileset dir= "${lib.dir } "> <include name=" **/*.jar "/> </fileset> </path> </pathconvert>

In addition, when you create manifest files, add:

<!--This allows you to add a third-party jar pack to the--> <attribute name= "Class-path" value= "${libs.project}"/> <!--main classes that run--> < Attribute name= "Main-class" value= "com.**.checking. Checking_start "/>

This runs ant, and the MANIFEST.MF in the jar package is as follows:

manifest-version:1.0 ant-version:apache Ant 1.7.0 CREATED-BY:1.5.0_09-B01 (Sun Microsystems Inc.) Implementation-title:fee task implementation-version:1.0 Implementation-vendor:aspire Main-Class: Com.aspire.cmgp.flowcontrol.server.FlowControlServer Class-path:lib/cmgp-util-1.0.1.jar lib/ Commons-codec-1.3.jar lib/comm ons-collections.jar lib/commons-dbcp-1.2.1.jar lib/commons-httpclient. Jar lib/ Commons-logging.jar Lib/commons-pool-1.2.jar Lib/dom4j.jar L Ib/log4j.jar Lib/ojdbc14.jar

In this case, there is no need to specify the jar package required by the program in the shell script, and there is no annoying problem with setting the environment variable. More formal is also the operation of this.
This way you can run the jar package directly in the shell: Java-jar the main program. JAR-XMX1024M-XMS1024M-XMN512M, a lot of convenience ~

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.