Home  >  Article  >  Java  >  Example of Shell calling Java/Jar program execution

Example of Shell calling Java/Jar program execution

黄舟
黄舟Original
2017-08-20 09:15:161872browse

这篇文章主要介绍了Shell执行/调用Java/Jar程序例子的实例详解的相关资料,这里提供实例帮助大家实现这样的功能,需要的朋友可以参考下

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代码 


 
 
  
   
  
  
  
 
  
  
  
  
  
  
  
  
 
 
  
  
   
  
  
  
 
 
  
     
     
     
  
 
  
   
   
   
  
 
  
   
   
    
     
      
     
    
   
   
  
 
  
   
  
 
  
  
   
   
    
   
  
   
  
   
   
   
 
   
    
     
    
   
   
    
   
   
   
       
         
       
   
   
   
  
 
  
   
    
     
     
     
    
    
     
    
   
   
  
  
  
   
   
    
    
     
      
     
    
   
  
 
  
 
  
   
   
   
  
 

    由于比较简单,这里就不多做注释,完整例子,请见附件.使用的话,只要进入你的工程,运行ant package,便会将程序打包到$YOUR_PROJECT/dist目录下.之后进入该程序运行bin/shell-java-ant  a b c d e f g,就会得到arguments length:7
这样子的输出.

The above is the detailed content of Example of Shell calling Java/Jar program execution. 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