Package Jar files into Linux executable files and linuxjar files

Source: Internet
Author: User

Package Jar files into Linux executable files and linuxjar files

Because I don't want to write java-jar jarfile, I want to directly write./jarfile to run the program.

 

#!/bin/bash - #===============================================================================##          FILE: shell_pack.sh# #         USAGE: ./shell_pack.sh # #   DESCRIPTION: # #       OPTIONS: ---#  REQUIREMENTS: ---#          BUGS: ---#         NOTES: ---#        AUTHOR: lwq (28120), scue@vip.qq.com#  ORGANIZATION: #       CREATED: 04/22/2015 02:38:01 PM CST#      REVISION:  ---#===============================================================================#===  FUNCTION  ================================================================#         NAME:  usage#  DESCRIPTION:  Display usage information.#===============================================================================function usage (){        cat <<- EOT  Usage :  $0 [options] -p package -s script file1 file2 file3 ..  Options:  -h|help       Display this message  -p|package    The output package name  -j|jar file   The jar fileEOT}    # ----------  end of function usage  ----------#-----------------------------------------------------------------------#  Handle command line arguments#-----------------------------------------------------------------------while getopts ":hp:j:" optdo  case $opt in    h|help    ) usage; exit 0   ;;    p|package ) package_name=$OPTARG ;;    j|jar     ) jar_file=$OPTARG ;;    \?        ) echo -e "\n  Option does not exist : $OPTARG\n"          usage; exit 1   ;;  esac    # --- end of case ---doneshift $(($OPTIND-1))if [[ -z $package_name ]]; then    echo "package_name can't not be empty"    usage    exitfiif [[ -z $jar_file ]]; then    echo "jar_file can't not be empty"    usage    exitfigenerate_wrapper_script(){    local wrapper_script=$1    cat <<-'EOT' >$wrapper_script#!/bin/shjava -jar $0 "$@"if [ $? -ne 0 ]; then    echo "run jar file failed."    exit 1fiexit 0#__SCRIPTEND__EOT}wrapfile=wrap_$$.shecho -e "start packing ..\n"generate_wrapper_script $wrapfilecat $wrapfile $jar_file > $package_namechmod +x $package_nameecho -e "\noutput: $package_name\n"rm -f $wrapfile

Save it to jar_wrapper.sh. Example:

./Jar_wrapper.sh-p/tmp/aaa-j/tmp/test_demo1.jar

Then, chmod + x/tmp/aaa &/tmp/aaa will be able to run the program ~!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.