Shell执行/调用Java/Jar程序例子的实例详解
程序员文章站
2022-06-02 09:31:12
shell执行/调用java/jar程序例子的实例详解
前言:
最近要写一个独立的java程序去监控hadoop和oozie,通过shell去调用.写代码到现在也4年多...
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
这样子的输出.
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!