Shell執(zhí)行/調用Java/Jar程序例子的實例詳解
前言:
最近要寫一個獨立的Java程序去監(jiān)控Hadoop和Oozie,通過Shell去調用.寫代碼到現(xiàn)在也4年多了,貌似就從來沒在生產(chǎn)環(huán)境中寫過一個獨立的Java程序,不是部署到Tomcat就是直接丟給Hadoop.于是參考Hadoop等開源環(huán)境,自己寫了一個demo,并且可以通過Ant打包生成可運行的程序.所以這里有三步:Java程序,Shell,Ant
1.首先建立Java程序,由于是例子,所以這里很簡單,只是輸出傳入?yún)?shù)的個數(shù),
代碼如下:
package com.guoyun.study.shell;
public class ShellJavaAnt {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("arguments length:"+args.length);
}
}
2.Shell腳本
Shell代碼
#!/bin/bash
#
PRG="${0}"
while [ -h "${PRG}" ]; do
ls=`ls -ld "${PRG}"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "${PRG}"`/"$link"
fi
done
BASEDIR=`dirname ${PRG}`
BASEDIR=`cd ${BASEDIR}/..;pwd`
if test -z ${JAVA_HOME}
then
JAVA_BIN=java
else
JAVA_BIN=${JAVA_HOME}/bin/java
fi
CLASS_PATH=$BASEDIR/shell-run-java-by-ant-*.jar
SERVICE_CLASS="com.guoyun.study.shell.ShellJavaAnt"
${JAVA_BIN} -cp $CLASS_PATH ${SERVICE_CLASS} "${@}"
exit 0
3.ANT
Xml代碼
project name="shell-run-java-by-ant" default="release-jar" basedir=".">
property file="build.properties" />
property name="jar.name" value="shell-run-java-by-ant" />
property name="version" value="0.1" />
property name="package.name" value="${jar.name}-${version}" />
property name="build.dir" location="build" />
property name="build.cache.dir" location="${build.dir}/cache" />
property name="build.lib.dir" location="${build.dir}/lib" />
property name="javadoc.dir" location="javadoc" />
property name="dist.dir" location="dist" />
property name="dist.java.dir" location="${dist.jar}/java" />
property name="package.dir" location="${dist.dir}/${package.name}" />
property name="tar.file" location="${dist.dir}/${jar.name}-${version}.tar.gz" />
property name="src.dir" location="src" />
property name="java.src.dir" location="${src.dir}/java" />
property name="test.src.dir" location="${src.dir}/test" />
property name="classes.dir" location="${build.dir}/classes" />
property name="lib.dir" value="${basedir}/lib"/>
property name="script.dir" value="${basedir}/script"/>
target name="release" description="sets properties for release builds.">
echo message="Building in release mode..."/>
property name="compile.mode" value="release"/>
property name="java.debug" value="false"/>
/target>
target name="init">
tstamp/>
mkdir dir="${build.dir}" />
mkdir dir="${classes.dir}" />
/target>
macrodef name="compile-source">
sequential>
javac srcdir="${java.src.dir}" destdir="${classes.dir}" debug="${java.debug}" debuglevel="lines,vars,source">
classpath>
fileset dir="lib">
include name="**/*.jar"/>
/fileset>
/classpath>
/javac>
/sequential>
/macrodef>
target name="compile" depends="init" description="compile the source">
compile-source />
/target>
target name="jar" depends="compile">
delete file="${build.dir}/${jar.name}-${version}.jar"/>
jar jarfile="${build.dir}/${jar.name}-${version}.jar">
fileset dir="${classes.dir}"/>
/jar>
/target>
target name="package" depends="clean, jar">
mkdir dir="${package.dir}"/>
mkdir dir="${package.dir}/lib"/>
mkdir dir="${package.dir}/bin" />
copy todir="${package.dir}/lib" includeEmptyDirs="false">
fileset dir="${lib.dir}">
include name="**/*" />
/fileset>
/copy>
copy todir="${package.dir}">
fileset file="${build.dir}/${jar.name}-${version}.jar"/>
/copy>
copy todir="${package.dir}/" file="build.xml"/>
copy todir="${package.dir}/bin" includeEmptyDirs="false">
fileset dir="${basedir}/script/shell">
include name="**/*" />
/fileset>
/copy>
chmod file="${package.dir}/bin/*" perm="ugo+rx"/>
/target>
target name="tar" depends="package" description="Create release tarball">
tar compression="gzip" longfile="gnu" destfile="${tar.file}">
tarfileset dir="${package.dir}" mode="777" prefix="${package.name}">
exclude name="bin/*" />
exclude name="**/*.sh" />
include name="**" />
/tarfileset>
tarfileset dir="${package.dir}" mode="777" prefix="${package.name}">
include name="**/*.sh" />
/tarfileset>
/tar>
delete dir="${package.dir}" />
/target>
target name="javadoc" depends="compile" description="generate documentation" >
delete dir="${javadoc.dir}" />
javadoc destdir="${javadoc.dir}">
fileset dir="${java.src.dir}" />
classpath>
fileset dir="${lib.dir}">
include name="**/*.jar"/>
/fileset>
/classpath>
/javadoc>
/target>
target name="release-jar" description="Build release and jar" depends="release,jar"/>
target name="clean" description="clean up">
delete dir="${build.dir}"/>
delete dir="${classes.dir}" />
delete dir="${dist.dir}"/>
/target>
/project>
由于比較簡單,這里就不多做注釋,完整例子,請見附件.使用的話,只要進入你的工程,運行ant package,便會將程序打包到$YOUR_PROJECT/dist目錄下.之后進入該程序運行bin/shell-java-ant a b c d e f g,就會得到arguments length:7
這樣子的輸出.
如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:- Linux啟動jar包的shell腳本
- 詳解spring boot 以jar的方式啟動常用shell腳本
- shell腳本運行java程序jar的方法
- Linux 啟動停止SpringBoot jar 程序部署Shell 腳本的方法
- Linux下shell通用腳本啟動jar(微服務)
- Linux利用Shell腳本部署jar包項目的完整步驟