In many Java projects, it is often seen that program custom parameters call the Java command packaging shell script. For example,
$ ANT_HOME/BIN/ANT, $ Groovy_home/Bin/Groovy
, Even in our TimeMachine Scheduler program, can be seen
$etmachine_home/bin/scheduler.sh
It is boring and easy to make these packaging scripts. Most problems come from setting the correct classpath to the program. If you are developing internal projects for a company, then you may stay away from the tangled path and environmental variable problem. But for open source projects, people need to make packaging more flexible and universal. Most of the .bat versions were provided. Windows DOS is indeed a brutal and restricted terminal without meeting your project script needs well. Therefore, I often encourage others to use Cygwi as much as possible. At least it has a real Bash Shell. Other common problems are that these packaging will soon lose control and many redundant scripts will appear around your project.
Run-java packaging script introduction
If you see the code of $ Timemachine_home/Bin/Scheduler.sh, you will see that it is actually called the Run-Java script in the same directory.
DIR = $ (DIRNAME $ 0) scheduler_home = $ DIR/Run-java -DSCHEDULER.HOME = "$ scheduler_home" timemachine.tool.tool.dulerserver "" $@"
As you can see, our Run-Java can use the -D option, not only that, it can also use the -CP option! Moreover, you can specify these options behind Main Class! This can make Run-Java 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.groovymain Test.groovy, Without downloading the entire branch again.
You can easily use it in any directory, it confirms its directory and then automatically load any jar package in the lib directory. Now if you want to add more jar bags to run Groovy, you can use the -CP option as follows: CP options:
$ TimeMachine_Home/Bin/Run-Java -CP "$ Home/APPS/My-APP/Lib/*" Groovy.groovymain Test.groovyUsually, if you set up Java ClassPath, you often cause errors when you are not careful, but you can run it in advance with Run-Java:
Run_java_dry = 1 $ Timemachine_Home/Bin/Run-Java -cp "$ HOME/APPS/My-APP/Lib/*" Groovy.groovymain Test.groovy
You only need to run the above line code in the command prompt line. It will output the Java commands of all options and parameters with a complete output.
Run-Script also contains a lot of other options, and you can understand it by viewing its annotation. The current script can run in any Linux Bash and Windows Cygwin.
Use Run-Java through Maven in development
According to the example mentioned above, assuming the project release structure is as follows:
$ Timemachine_home +-Bin/Run-Java +-Lib/*. Jar
But what is the directory during the development process? A common case is: you want to run the latest compiled code under TARGET/CLASSES instead of packing or publishing the entire project. You can also use Run-Java in this case. First of all, 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 only need to do this. Run-Java will automatically detect these directory and create the correct classpath for your main class.
If you use Eclipse to develop, then your target/classs directory will always be updated, and Run-Java can become a treasure in your project development.
Get Run-Java packaging script
#!/USR/BIN/ENV Bash ## Copyright 2012 Zemian deng ## Licensed Under The Apache License, Version 2.0 (The "License");# You May Not Use This File in compliance with the livense.# You May Obtain a Copy of the License at ## http://www.apache.org/licenses/6license-2.0## unless required by Applicable Law or Agreed to In Writing, Software# ED Under The License is Distributed on an "as is" BASIS,# Without Warranties or Conditions of Any Kind, Either Express or Implied.# See the License for the Specific Language Governing Permissions and# Limit ATIONS Under the License.# A Wrapper Script that run any java6 application in unix/cygwin env. ## This script is assumed to be located in an application's "bin" directory. It will# Auto Resolve Anymbolic Link and Alway in Relacted to This Applicat. ION# DIRECTORY (which is one paint up from the script.) TheReface, this script can be # Run any where in the file system and it will still reference this application# Directory. s up any "config"# and "lib" dia "directories under the application directory. It also Will Also Add A# ANY TYPICAL MAVEN PROJECT OUTPUT DIRECTORIES SUCH As "Target/Test-Classes",# "Target/Classes", and "Target/Dependency" h. This can be disable by# setting run_java_no_parse = 1. ## if The "Default Parameters. s# Profile Startup Script. The values of all path should be in cygwin/unix path,# And this script will auto convert the Windows Path WHERE is Needed. ## User May Customize the Java Classpath By Setting Run_java_cp, Which Will Prefix to Existing# classpath, or use the "-cp" option, which will postfix to existing classpath. ## usage:# Run-java [java_opts] <java_main_class> [-cp /more /classpath] [-dsysprop = value] ## Example:# Run-Java Example.Hello# Example.hello -DNAME = World # Run-java org.junit.runner.junitcore exmple.hellotest -cp "C: /Apps/lib/junit4.8.2/*" ## Created by: Zemian deng 03/09/2012# This Run Script D D IR (Resolve to absolute path) Script_dir = $ (CD $ (DIRNAME $ 0) && PWD) # This Dir is where this script live.app_dir = $ (CD $ script_dir/.. &&PD) # assume The Application Dir is One Level Up FROM Script Dir # Default parametersjava_home = $ {java_home: =/apps/jdk} # this is the home directory of java development = $ Classpath} # a classpath prefix before -Classpath Option, default to $ classpathrun_java_opts = $ {Run_java_opts: =} # java options (-xmx512m -xx: maxpermsize = 128m ETC) Run_java_debug = $ {run_java_debug: =} E 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_autocp: =} # If Noty , do not auto setup java classpathrun_java_dry = $ {run_java_dry: =} # if not Empty, do not exec java commit, but just proprint # os special support. $ var _must_ be set to either true or facese.cygwin = false; "unaame`" in cygwin " *) Cygwin = TRUE;; ESAC # DEFINE WHERE Is the java Executable isjava_cmd = javaif [-d "$ java_home"]; then java_cmd = "$ java_home/bin/java" fi # auto setup applciation's java (only if they exists) if [-z "$ run_java_no_autocp"]; Then if $ cygwin; then #PRovide Windows Directory Conversion Java_HOME_WIN = $ (Cygpath -AW "$ java_home") app_dir_win = $ (cygpath -AW "$ 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 -class "; fi if [-d" $ App_dir_win/target/class "]; then run_java_cp =" $ Run_java_cp; $ app_dir_win/target/class "; Cy "]; 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/*"; "]; then run_java_cp = "$ RUN_JAVA_CP: $ app_dir/config"; fi if [-d "$ app_dir/target/test -class"]; -Classes "; fi if [-d "$ APP_DIR/TARGET/CLASSES"]; then run_java_cp = "$ Run_java_cp: $ app_dir/target/class"; 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/*"; D "after The Java Main Class from Script Arguments# This is Done for Convenient Sake so users not to export run_java_cp and run_java_opts# Ey Can Pass Into End of This Run-Java Script Instead.# this can be disable by self_java_parse = 1.IF [-z "$ run_java_no_parse"]; then # Prepare variables for PARSING FORIND_CP = Declare -A New_args IDX = 0 # Parse All Arguments and LOOK for "-CP" and "for arg in" $@ "; do if [[ -n $FOUND_CP ]]; then if [ "$OS" = "Windows_NT" ]; then # Can't use cygpath here, because cygpath will auto expand "*", which we do not # want . User will just have to use the os Path When Specifying "-CP" Option. #Arg = $ (Cygpath -W -A $ arg) Run_java_cp = "$ Run_java_cp; P = "$ 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" Lee T IDX = $ IDX +1;; ESAC FI DONE # Display 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 userd variables if [-z" $ run_java_dry "]; then" $ java_cmd "$ run_java_opts -CP" $ run_java_cp "" "" "" "" "" "" "" "" "" "" "" $ {New_args [@]} "fielse # display FULL JAVA Command. If [-n "$ run_java_debug"] || [-n "$ run_java_dry"]; then echo "$ java_cmd" $ run_opts -CP "$ run_java_cp" "$@" fi # "Fi # Run java main class if [ -z "$ run_java_dry"]; then "$ java_cmd" $ run_java_opts -cp "$ run_java_cp" "$@" fifi