Shell执行/调用Java/Jar程序例子的实例详解
前言:
最近要写一个独立的Java程序去监控Hadoop和Oozie,通过Shell去调用.写代码到现在也4年多了,貌似就从来没在生产环境中写过一个独立的Java程序,不是部署到Tomcat就是直接丢给Hadoop.于是参考Hadoop等开源环境,自己写了一个demo,并且可以通过Ant打包生成可运行的程序.所以这里有三步:Java程序,Shell,Ant
1.首先建立Java程序,由于是例子,所以这里很简单,只是输出传入参数的个数,
代码如下:
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
这样子的输出.
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!