This article mainly introduces the method of running java program jar by shell script. The editor thinks it is quite good. Now I will share it with you and give it as a reference. Let’s follow the editor and take a look.
When deploying projects on UBuntu, we often start the program through a shell, or even call the java program regularly through the crontab scheduled task, but there is a very strange problem That is, for example, I wrote the following shell script:
#!/bin/sh export mypath=/root/project/wishnomal java -Xmx3000m -Xms3000m -server -d64 -Dfile.encoding=UTF-8 -Dfetch.threads=300 -classpath $mypath/:$mypath/wish2-assembly-1.0.0.jar newstandard.CrawlerNewStandard $* echo "END"
When I run the script manually from the command line, I can run the java program normally, but using crontab scheduled tasks, it seems that It won’t work
Analysis of possible reasons:
1) Whether the current user does not have executable permissions for this shell script, use ls -lrt /apps/service/mtk/checking/run. sh checks that the script is executable, but it has execution permission -rwxr-xr-x
2) Since there is no problem running the script alone, is it a timing problem? So I wrote a simple output shell script and it was no problem through timing. The problem is still with the script.
Later I checked online and thought it might be the environment variables in the script, because when running the script through crontab, the root user is used instead of the current user, so I cat /etc/profile to check the environment variables and then modify them. The script is as follows:
Analysis of possible reasons:
1) Whether the current user does not have executable permissions for this shell script, check through ls -lrt /apps/service/mtk/checking/run.sh The script is executable, but it has execution permission -rwxr-xr-x
2) Since there is no problem running the script alone, is it a timing issue? So I wrote a simple output shell script and it was no problem through timing. The problem is still with the script.
Later I checked online and thought it might be the environment variables in the script, because when running the script through crontab, the root user is used instead of the current user, so I cat /etc/profile to check the environment variables and then modify them. The script is as follows:
#!/bin/sh export mypath=/root/project/wishnomal export JAVA_HOME=/root/lib/jdk1.7.0_72 PATH=$PATH:$JAVA_HOME/bin java -Xmx3000m -Xms3000m -server -d64 -Dfile.encoding=UTF-8 -Dfetch.threads=300 -classpath $mypath/:$mypath/wish2-assembly-1.0.0.jar newstandard.CrawlerNewStandard $* echo "END"
export displays the environment variables exported as user environment variables
In this way, the crontab scheduled tasks will be normal.
Modification reference:
#!/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 &
When you run the script manually from the command line, you can run the java program normally, but it seems impossible to use crontab scheduled tasks. It worked, but I was very depressed. I checked the reason and analyzed the possible reasons:
1) Whether the current user does not have executable permissions for this shell script, use ls -lrt /apps/service/mtk/checking/run .sh shows that the script is executable, but it has execution permission -rwxr-xr-x
2) Since there is no problem running the script alone, is it a timing problem? So I wrote a simple output shell script and it was no problem through timing. The problem is still with the script.
Later I checked online and thought it might be the environment variables in the script, because when running the script through crontab, the root user is used instead of the current user, so I cat /etc/profile to check the environment variables and then modify them. The script is 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 displays the environment variables exported as user environment variables
The above jar package is exported through the eclipse tool export. The MANIFEST.MF file is not included. If we use the packaging tool Ant, we can set Class-Path
in the default build.xml file and add the third-party jar package to the manifest.mf file, and Specify the main class of the program
Add the following content in build.xml:
<!-- create a property containing all .jar files, prefix lib/, and seperated with a space --> <pathconvert property="libs.project" pathsep=" "> <mapper> <chainedmapper> <!-- remove absolute path --> <flattenmapper /> <!-- add lib/ prefix --> <globmapper from="*" to="lib/*" /> </chainedmapper> </mapper> <path> <!-- lib.home contains all jar files, in several subdirectories --> <fileset dir="${lib.dir}"> <include name="**/*.jar" /> </fileset> </path> </pathconvert>
In addition, when creating the manifest file, add:
<!-- 这样就可以将第三方jar包加入 --> <attribute name="Class-Path" value="${libs.project}" /> <!-- 程序运行的主类 --> <attribute name="Main-Class" value="com.**.checking.Checking_Start " />
Run ant like this, the content of MANIFEST.MF in the resulting 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
Like this There is no need to specify the jar package required by the program in the shell script, and there is no annoying problem of setting environment variables. This is how the more formal ones operate.
Just run the jar package directly in the shell: java -jar main program.jar -Xmx1024m -Xms1024m -Xmn512m, append # after
#!/bin/bash
##source /etc/profilesource ~/.bash_profile
#! /bin/sh export JAVA_HOME=/usr/java/jdk1.6.0_18 export CLASSPATH=.:${JAVA_HOME}/lib/dt.jar:${JAVA_HOME}/lib/tools.jar for i in lib/*.jar; do CLASSPATH=$i:${CLASSPATH} done export CLASSPATH=.:${CLASSPATH}java -cp ${CLASSPATH} The package name where the main method is located. The class name where the main method is located
The above is the detailed content of Shell script implements method of running Java program jar. For more information, please follow other related articles on the PHP Chinese website!