I've been using it for a while off and on, when I'm working on something that won't wait until I have a computer but doesn't warrant carrying a machine with me (like on a backpacking trip). Its power and simplicity make it perfect for a little on-the-go coding as ideas come to mind.
One part that wasn't so simple when I first got started was the builder. I found it to be a little clunky to use. There is another builder script here, along with a few other utilities. That one has lots of options.
Below, I'm posing a builder that I wrote some time ago that is very simple and only has a couple options. It has served me well so perhaps you'll also find it useful.
The simplest way to build and execute your code is to place the script in your home directory (~). Then, cd into the directory containing your .java file, and run this line:
. ~/builder.sh -l
For the help file, you can always run: . ~/builder.sh -h
Here is the entire script:
#!~/system/bin/bash
#Simple Build Script
#For some reason the getopts index needs to be reset manually...
OPTIND=0compile=1
javaFilename=""
do
case $opt inf)
javaFilename=${OPTARG%.*}
;;
l)
launch=1
;;
o)
compile=0
launch=1
;;
\? | h)
usage
return
;;
esac
done
{
scriptname=builder.sh
cat << EOF
usage: $scriptname [ hlo ] [ f filename.java ]
-h Show this message
-l Launch file after building
-o Only launch, no build
-f Specify a file instead of finding the java file in the current directory
To build the java file in the current directory: $scriptname
To build and launch: $scriptname -l
To launch only: $scriptname -o
To specify a file to build and launch: $scriptname -lf filename.java
# Bring in my utilities classes
../utilities}
#Find java javaFilename if none given
if [[ $javaFilename == "" ]]; then
if [ `ls -l |grep -c .java` != 1 ]; then
echo Because there is not a solitary java file in the directory, you must specify a filename using the -f switch.return
fi
javaFilename=`find . -name *.java`
javaFilename=${javaFilename:2:-5}
else
if [ ! -f ${javaFilename}.java ]; thenecho File ${javaFilename}.java not found.
return
fi
fi
includeList=""
delimiter=""
#Prepare to include other classes, if specified.
if [ -f includes ]; then
while read line; do
if [ "${line}" != "" -a "${line:0:1}" != "#" ]; then
includeList=${includeList}" "${line}"/*.class"compileList=${compileList}${delimiter}${line}
delimiter=":"
fi
done < includes
else
echo No \"includes\" file.
fi
#Clean up. This does not clean up inner class classfiles but does allow for multiple class files in the same directory to be included in the generated jar.
if [ -f ${javaFilename}.class ]; thenrm ${javaFilename}.class
fi
if [ -f ${javaFilename}.jar ];thenrm ${javaFilename}.jar
fi
#Compile java file into classfile(s).
if [ -z $compileList ]; thenjavac -verbose ${javaFilename}.java
else
javac -verbose ${javaFilename}.java -classpath $compileList
fi
#Now convert to dex file. The assumption is you want to include all class files in the directory.
dx --dex --verbose --output=${javaFilename}.jar ./*.class $includeList
fi
#Finally, launch if specified.
if [ $launch -eq 1 ]; thenjava -jar ${javaFilename}.jar $javaFilename
fi