当前位置: 移动技术网 > IT编程>开发语言>Java > shell脚本运行java程序jar的方法

shell脚本运行java程序jar的方法

2019年07月19日  | 移动技术网IT编程  | 我要评论

在ubuntu上部署项目的时候,我们往往通过一段shell来启动程序,甚至是通过crontab定时任务来定时的调用java程序,但是很奇怪的一个问题就是,比如我写了一个如下的shell脚本:

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

手动命令行运行该脚本的时候,可以正常运行java程序,但是使用crontab定时任务,貌似就不起效果了

分析可能原因:

 1)是否当前用户对此shell脚本没有可执行权限,通过ls -lrt /apps/service/mtk/checking/run.sh查看脚本是有可执行,但是有执行权限呀-rwxr-xr-x

 2)既然单独运行脚本没问题,那会不会是定时的问题呢?于是写了一个简单的输出的shell脚本通过定时也是没问题的。说明还是脚本的问题。

后来上网查了下,想到可能是脚本中环境变量的原因,因为通过crontab运行脚本,是以root用户,而不是当前用户,于是cat /etc/profile查看环境变量,然后修改脚本如下:

分析可能原因:

 1)是否当前用户对此shell脚本没有可执行权限,通过ls -lrt /apps/service/mtk/checking/run.sh查看脚本是有可执行,但是有执行权限呀-rwxr-xr-x

 2)既然单独运行脚本没问题,那会不会是定时的问题呢?于是写了一个简单的输出的shell脚本通过定时也是没问题的。说明还是脚本的问题。

后来上网查了下,想到可能是脚本中环境变量的原因,因为通过crontab运行脚本,是以root用户,而不是当前用户,于是cat /etc/profile查看环境变量,然后修改脚本如下:

#!/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显示导出为用户环境变量的环境变量

这样crontab计划任务就正常了。 

修改参考:

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

手动命令行运行该脚本的时候,可以正常运行java程序,但是使用crontab定时任务,貌似就不起效果了,很是郁闷哪,查原因哪,分析可能原因:

 1)是否当前用户对此shell脚本没有可执行权限,通过ls -lrt /apps/service/mtk/checking/run.sh查看脚本是有可执行,但是有执行权限呀-rwxr-xr-x

 2)既然单独运行脚本没问题,那会不会是定时的问题呢?于是写了一个简单的输出的shell脚本通过定时也是没问题的。说明还是脚本的问题。

后来上网查了下,想到可能是脚本中环境变量的原因,因为通过crontab运行脚本,是以root用户,而不是当前用户,于是cat /etc/profile查看环境变量,然后修改脚本如下:

#!/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显示导出为用户环境变量的环境变量

以上这种jar包是通过eclipse工具export导出,不包含manifest.mf文件,如果使用打包工具ant,我们可以在打包默认的build.xml文件中设置class-path

将第三方jar包加入manifest.mf文件中,且指定程序主类

在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> 

另外,在create manifest文件时,加上:

<!-- 这样就可以将第三方jar包加入 -->  
<attribute name="class-path" value="${libs.project}" /> 
<!-- 程序运行的主类 --> 
<attribute name="main-class" value="com.**.checking.checking_start " /> 

这样运行ant,打成的jar包中manifest.mf中内容如下:

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 

这样在shell脚本中就不需要指定程序所需要的jar包了,也就不存在环境变量设置的恼人问题。比较正规的也是这么操作的。

这样在shell中就直接运行jar包就行了:java -jar 主程序.jar -xmx1024m -xms1024m -xmn512m,

#!/bin/bash后追加

source /etc/profile
source ~/.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} main方法所在包名.main方法所在的类名

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网