Introduction to Java Program Shell script wrapper _java

Source: Internet
Author: User
Tags wrapper

In many Java projects, you will often see a wrapper shell script that invokes the Java command with the program's custom parameters. For example

$ANT _home/bin/ant, $GROOVY _home/bin/groovy

, even in our TimeMachine scheduler program.

$TIMEMACHINE _home/bin/scheduler.sh

Writing these wrapper scripts is tedious and error prone. Most of the problems come from setting the correct classpath for the program. If you are developing an internal project for a company, you may be able to stay away from tangled paths and environmental variables. But for open source projects, people need to make packaging more flexible and versatile. Most even provide a. bat version. Windows DOS is really a barbaric and restricted terminal that does not meet your project scripting needs well. Therefore, I often encourage others to use CYGWI as much as possible. At least it has a real bash shell. Another common problem is that these packages will soon be out of control and there will be a lot of redundant scripts going on throughout your project.

Run-java Packaging Script Introduction

If you see $TIMEMACHINE _home/bin/scheduler.sh code, you will see that it is actually looping through the Run-java script in the same directory.

dir=$ (dirname $)
scheduler_home= $DIR/.
$DIR/run-java-dscheduler.home= "$SCHEDULER _home" Timemachine.scheduler.tool.SchedulerServer "$@"

As you can see, our Run-java is able to use the-D option, and not only that, it can also use the-CP option! Also, you can specify these options after the main class! This allows Run-java to be packaged by other scripts, and can still add additional system attributes and classpath.

For example, TimeMachine comes with the Groovy library, so you can simply call it like this

Groovy: $TIMEMACHINE _home/bin/run-java groovy.ui.GroovyMain Test.groovy

Instead of downloading the entire branch again.

You can easily use it in any directory, it confirms its own directory and can automatically load any jar packages under the Lib directory. Now if you want to add more jar packs to run groovy, you can use the-CP option as follows:

$TIMEMACHINE _home/bin/run-java-cp "$HOME/apps/my-app/lib/*" Groovy.ui.GroovyMain Test.groovy
Usually if you set the Java classpath not careful enough it often leads to errors, but you can run it one time with Run-java:

Run_java_dry=1 $TIMEMACHINE _home/bin/run-java-cp "$HOME/apps/my-app/lib/*" Groovy.ui.GroovyMain Test.groovy

You only need to run the entire line of code on the command prompt line. It will output the complete Java command with all the options and parameters.

Run-script also contains a number of other options that you can learn by viewing its annotations. The current scripts can be run in any Linux bash and Windows Cygwin.


Using MAVEN with Run-java in development

Based on the examples mentioned above, assume that the project publishing structure is as follows:

$TIMEMACHINE _home
 +-bin/run-java
 +-Lib/*.jar

But what about the catalog during the development process? A common use case is that you want to be able to run the newly compiled code under target/classes rather than package or publish the entire project. You can also use Run-java in this kind of situation. First, simply add Bin/run-java to your project and then run

MVN Compile Dependency:copy-dependencies

All jar files will be generated under target/dependency. You just have to do this. Run-java will automatically detect these directories and create the correct classpath for your main class.

If you use eclipse to develop, your target/classes directory will always be updated, and Run-java will be the gem of your project development.

Get Run-java Wrapper script

#!/usr/bin/env Bash # # Copyright Zemian Deng # Licensed under the Apache License, Version 2.0 (the "License");
# You could not use this file, except in compliance with the License.  # You may obtain a copy of the License in # # http://www.apache.org/licenses/LICENSE-2.0 # unless required by applicable Writing, software # Distributed under the License is distributed on ' as is ' basis, # without Warra
Nties or CONDITIONS of any KIND, either express OR implied.
 
# The License for the specific language governing permissions and # Limitations under the License.
# A Wrapper script that run any JAVA6 application in Unix/cygwin env. # This the script is assumed to being located in a application ' bin ' directory. It'll # Auto resolve any symbolic link and always run in relative to this application # directory (which is one parent u p from the script.) Therefore, this script can is # Run any where in the file system and it'll still reference this application # diRectory. # This script would by default Auto setup a Java classpath which picks up any "config" # and "Lib" directories under the A Pplication directory. It also'll also add a # any typical Maven project output directories such as "Target/test-classes", # "Target/classes", and "Target/dependency" into classpath.
This can is disable by # setting run_java_no_parse=1. # If the ' Default parameters ' section bellow doesn ' t match to User's env, then user # may override this variables in th EIR Terminal session or preset them in Shell "s # Profile startup script. The values of all path should being in Cygwin/unix path, # and this script would auto convert them into Windows path where is
Needed. # User may customize the Java classpath by setting RUN_JAVA_CP, which'll prefix to existing # classpath, or use the '-
CP "option, which'll postfix to existing classpath. # Usage: # Run-java [java_opts] <java_main_class> [-cp/more/classpath] [-dsysprop=value] # Example: # Run-java Example.Hello # Run-java Example. Hello-dname=world # Run-java Org.junit.runner.JUnitCore example. HELLOTEST-CP "c:\apps\lib\junit4.8.2\*" # Created By:zemian Deng 03/09/2012 # This run script dir (resolve to Absolu
Te path) script_dir=$ (CD $ (dirname $) && pwd) # This DIR was where this SCRIPT live.
 
app_dir=$ (CD $SCRIPT _dir/. && pwd) # Assume the application DIR is one level up from SCRIPT DIR.
# Default Parameters Java_home=${java_home:=/apps/jdk} # This is the home directory of JAVA Development Kit. run_java_cp=${run_java_cp:= $CLASSPATH} # A CLASSPATH prefix before-classpath option, default to $CLASSPATH run_java_opts =${run_java_opts:=} # JAVA options (-xmx512m-xx:maxpermsize=128m etc) run_java_debug=${run_java_debug:=} # If not emp
Ty, print the full Java command line before executing it. run_java_no_parse=${run_java_no_parse:=} # If not empty, skip the auto parsing of-d and-cp options from script arguments
. run_java_no_autocp=${run_java_no_autocp:=} # If NOt empty, don't auto setup java classpath run_java_dry=${run_java_dry:=} # If not empty, does not exec Java command, but Just print # OS specific support.
$var _must_ is set to either true or false.
Cygwin=false;
Case "' uname '" in cygwin*) cygwin=true;; ESAC # Define where is the Java executable is Java_cmd=java if [-D "$JAVA _home"]; Then java_cmd= "$JAVA _home/bin/java" Fi # Auto Setup applciation ' s JAVA Classpath (only if they exists) if [-Z "$RUN _j AVA_NO_AUTOCP "]; Then if $CYGWIN; Then # provide Windows directory conversion java_home_win=$ (cygpath-aw "$JAVA _home") app_dir_win=$ (cygpath-a W "$APP _dir") if [-D "$APP _dir_win\config"]; Then run_java_cp= "$RUN _java_cp; $APP _dir_win\config"; fi if [d "$APP _dir_win\target\test-classes"]; Then run_java_cp= "$RUN _java_cp; $APP _dir_win\target\test-classes"; fi if [d "$APP _dir_win\target\classes"]; Then run_java_cp= "$RUN _java_cp; $APP _dir_win\target\classes"; fi if [d ' $APP _dir_win\target\dEpendency "]; Then run_java_cp= "$RUN _java_cp; $APP _dir_win\target\dependency\*"; fi if [d "$APP _dir_win\lib"]; Then run_java_cp= "$RUN _java_cp; $APP _dir_win\lib\*"; Fi else if [-D "$APP _dir/config"]; Then run_java_cp= "$RUN _java_cp: $APP _dir/config"; fi if [d "$APP _dir/target/test-classes"]; Then run_java_cp= "$RUN _java_cp: $APP _dir/target/test-classes"; fi if [d "$APP _dir/target/classes"]; Then run_java_cp= "$RUN _java_cp: $APP _dir/target/classes"; fi if [d "$APP _dir/target/dependency"]; Then run_java_cp= "$RUN _java_cp: $APP _dir/target/dependency/*"; fi if [d "$APP _dir/lib"]; Then run_java_cp= "$RUN _java_cp: $APP _dir/lib/*";  fi fi fi # Parse addition "-CP" and "-D" after the Java main class from script arguments # this are done for convenient Sake so users don't have to export RUN_JAVA_CP and run_java_opts # saparately, but now they can pass into the this RU
N-java script instead.
# This can is disable by setting run_java_no_parse=1. If [-Z]$RUN _java_no_parse "];  Then # Prepare variables for parsing found_cp= declare-a New_args idx=0 # Parse All arguments and look for "-CP" and "D" for ARG in "$@"; do if [[-N $FOUND _CP]]; Then if ["$OS" = "windows_nt"]; Then # Can ' t use Cygpath here, because cygpath'll auto expand "*", which we do not # want.  
        User'll just have to use OS path when specifying "-CP" option.
      #ARG =$ (Cygpath-w-a $ARG) run_java_cp= "$RUN _java_cp $ARG" Else run_java_cp= "$RUN _JAVA_CP: $ARG"
      Fi found_cp= else case $ARG in '-cp ') found_cp=1;;
      '-d ' * run_java_opts= "$RUN _java_opts $ARG";;
      * new_args[$IDX]= "$ARG" let idx= $IDX +1;;
  Esac fi Done # Display the full Java command. If [-N "$RUN _java_debug"] | | [-N "$RUN _java_dry"]; Then echo "$JAVA _cmd" $RUN _java_opts-cp "$RUN _java_cp" "${new_args[@]}" FI # RUN Java Main class using parsed variables if [-Z "$RUN _java_dry"];
  Then "$JAVA _cmd" $RUN _java_opts-cp "$RUN _java_cp" "${new_args[@]}" fi else # Display full JAVA command. If [-N "$RUN _java_debug"] | | [-N "$RUN _java_dry"]; Then echo "$JAVA _cmd" $RUN _java_opts-cp "$RUN _java_cp" "$@" Fi # RUN JAVA Main class if [-Z "$RUN _java_dry " ];
 Then "$JAVA _cmd" $RUN _java_opts-cp "$RUN _java_cp" "$@" fi fi

Related Article

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.