Run the Java program using shell scripts

Source: Internet
Author: User

When deploying a project on Linux, we often use a shell to start the program, or even regularly call the Java program through a crontab scheduled task. However, a strange problem is that, for example, I wrote the following shell script:

 

#! /Bin/sh <br/> # begin <br/> # Start script for the cmgp bosscontrol <br/> # $ ID: run_bosscontrol.sh, V 1.0 2007/11/06 exp $ <br/> # character <br/> # specify the character set <br/> lang = zh_cn.gbk export Lang <br/> run_home =. <br/> classpath = $ classpath: $ run_home/lib/checking. jar <br/> classpath = $ classpath: $ run_home/lib/ojdbc14.jar <br/> classpath = $ classpath: $ run_home/lib/commons-dbutils-1.1.jar <br/> classpath = $ classpath: $ run_home/lib/log4j-1.2.14.jar <br/> classpath = $ classpath: $ run_home/lib/dom4j-1.6.jar </P> <p> export classpath </P> <p> java com. **. checking. checking_start> log. out & 

 

When you run the script through a manual command line, you can run the Java program normally. However, when you use crontab to schedule tasks, it seems to be ineffective. It is very depressing. Check the causes and analyze the possible causes:

1) whether the current user does not have the executable permission for this shell script, through LS-LRT/apps/service/MTK/checking/run. SH: You can run the script, but you have the execution permission.-rwxr-XR-x

2) Since it is okay to run the script separately, will it be a timing problem? Therefore, it is okay to write a simple shell script output through timing. It indicates a script problem.

Later, I checked it online and thought it might be the cause of the environment variable in the script. Because running the script through crontab is a root user instead of the current user, so I checked the environment variable in CAT/etc/profile, then modify the script as follows:

#! /Bin/sh <br/> # begin <br/> # Start script for the cmgp bosscontrol <br/> # $ ID: run_bosscontrol.sh, V 1.0 2007/11/06 exp $ <br/> # export <br/> export Path =/apps/usr/Java/jdk1.5/bin: $ PATH <br/> export java_home =/apps/usr/Java/jdk1.5 <br/> export jre_home =/apps/usr/Java/jdk1.5/JRE <br/> export classpath =/apps/usr/Java/jdk1.5/lib: /apps/usr/Java/jdk1.5/JRE/lib: $ classpath <br/> run_home =/apps/service/checking <br/> classpath = $ classpath $ run_home/lib/checking. jar <br/> classpath = $ classpath: $ run_home/lib/ojdbc14.jar <br/> classpath = $ classpath: $ run_home/lib/commons-dbutils-1.1.jar <br/> classpath = $ classpath: $ run_home/lib/log4j-1.2.14.jar <br/> classpath = $ classpath: $ run_home/lib/dom4j-1.6.jar </P> <p> export classpath = $ classpath </P> <p> java com. **. checking. checking_start> log. out &

 

Export displays the environment variables exported as user environment variables
The preceding jar package is exported using the eclipse tool export and does not contain the manifest. MF file. If you use the packaging tool ant, you can set the class-path in the default build. xml file.
Add a third-party jar package to the manifest. MF file and specify the main program class
Add the following content to build. xml:

 

<! -- Create a property containing all. jar files, prefix lib/, and seperated with a space --> <br/> <pathconvert property = "libs. project "pathsep =" "> <br/> <mapper> <br/> <chainedmapper> <br/> <! -- Remove absolute path --> <br/> <flattenmapper/> <br/> <! -- Add lib/prefix --> <br/> <globmapper from = "*" to = "lib/*"/> <br/> </chainedmapper> <br/> </mapper> <br/> <path> <br/> <! -- Lib. home contains all jar files, in several subdirectories --> <br/> <fileset dir = "$ {Lib. dir} "> <br/> <include name = "**/*. jar "/> <br/> </fileset> <br/> </path> <br/> </pathconvert>

 

In addition, when creating the manifest file, add:

<! -- In this way, you can add third-party jar packages --> <br/> <attribute name = "Class-path" value = "$ {libs. PROJECT} "/> <br/> <! -- Main class for running the program --> <br/> <attribute name = "Main-class" value = "com. **. Checking. checking_start"/>

 

Run ant in this way. The contents of manifest. MF in the compiled jar package are as follows:

Manifest-version: 1.0 <br/> ant-version: Apache ant 1.7.0 <br/> created-by: 1.5.0 _ 09-b01 (Sun Microsystems Inc .) <br/> Implementation-title: Execution task <br/> Implementation-version: 1.0 <br/> Implementation-vendor: aspire <br/> main-class: COM. aspire. cmgp. flowcontrol. server. flowcontrolserver <br/> class-path: lib/cmgp-util-1.0.1.jar lib/commons-codec-1.3.jar lib/Comm <br/> ons-collections.jar lib/commons-dbcp-1.2.1.jar lib/commons-httpclient <br/>. jar lib/commons-logging.jar Library/commons-pool-1.2.jar lib/dom4j. jar L <br/> IB/log4j. jar lib/ojdbc14.jar

 

In this way, you do not need to specify the jar package required by the program in the shell script, so there is no annoying problem with environment variable settings. This is also the case for more formal operations.
In this way, you can directly run the jar package in the shell: Java-jar main program. jar-xmx1024m-xms1024m-xmn512m, much more convenient ~

 

 

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.