search
HomeJavajavaTutorialShell 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/profile

source ~/.bash_profile

Test. .


#! /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!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)