Shell script implements method of running Java program jar
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}
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Guide to Perfect Number in Java. Here we discuss the Definition, How to check Perfect number in Java?, examples with code implementation.

Guide to Random Number Generator in Java. Here we discuss Functions in Java with examples and two different Generators with ther examples.

Guide to Weka in Java. Here we discuss the Introduction, how to use weka java, the type of platform, and advantages with examples.

Guide to Smith Number in Java. Here we discuss the Definition, How to check smith number in Java? example with code implementation.

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

Guide to TimeStamp to Date in Java. Here we also discuss the introduction and how to convert timestamp to date in java along with examples.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4
