Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env bash
- build_dir='build'
- lib_dir='lib'
- dist_dir='dist'
- ###
- ### Okay, say that you have a normal Java project directory tree
- ### structure that looks like this:
- ###
- ### $ ls
- ### build lib src
- ###
- ### In your src directory you have some Java source files that
- ### are arranged in packages.
- ###
- ### src/com/something/app/ProgramMain.java
- ### src/com/something/app/utils/Support.java
- ###
- ### When those files get compiled into class files, the Java
- ### class files typically are placed in the build directory.
- ###
- ### build/com/something/app/ProgramMain.class
- ### build/com/something/app/utils/Support.class
- ###
- ### To execute your Main class from the command line you must
- ### include a lot of information for the class path such as
- ### the exact location of any external libraries, and every
- ### compiled class that your program needs to run. This can
- ### get very complicated.
- ###
- ### This script will simplify the execution of your compiled
- ### Java program. Just name this script after the main class of
- ### your program, and the script will do the rest for you.
- ###
- ### For example, if your program is called "ProgramMain", you
- ### should name this script that, or "programmain". Case does
- ### not matter.
- ###
- ### Make this script executable,
- ###
- ### $chmod +x ./programmain
- ###
- ### And run it like a regular linux program.
- ###
- ### $./programmain
- ###
- if [ -h $0 ]; then
- link=$(readlink $0)
- me=$(basename $link)
- path_of_this_script=`dirname $link`
- else
- me=$(basename $0)
- path_of_this_script=`dirname $0`
- fi
- path_of_this_script=${path_of_this_script/".\/"/"$(pwd)\/"}
- java=$(which java)
- [ -z $java ] && { echo "You have not installed Java"; exit 1; }
- build_dir=$path_of_this_script/$build_dir
- lib_dir=$path_of_this_script/$lib_dir
- dist_dir=$path_of_this_script/$dist_dir
- main_class=$(find $build_dir | grep -i $me)
- main_class=${main_class:${#build_dir}+1}
- main_class=${main_class/%.class/''}
- main_class=${main_class//\//'.'}
- lib=$(find $lib_dir)
- for file in $lib; do class_path=$class_path$file:; done
- build=$(find $build_dir)
- for dir in $build; do [ -d $dir ] && class_path=$class_path$dir:; done
- if [ -f $dist_dir/$me*.jar ]; then
- $java -jar $dist_dir/$me*.jar $@
- else
- $java -cp $class_path $main_class $@
- fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement