Friday, July 12, 2013

Terminal IDE - builder

There's an app for Android devices called Terminal IDE by Spartacus Rex. It's a command line development environment for Java and Android and appears to now be hosted in Google Code.

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=0

launch=0
compile=1
javaFilename=""

while getopts "f:hlo" opt
do
case $opt in
f)
       javaFilename=${OPTARG%.*}
       ;;
     l)
       launch=1
       ;;
     o)
       compile=0
       launch=1
       ;;
     \? | h)
       usage
       return
       ;;
     esac
done

usage()
{
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

You may specify class locations by listing them in a file named "includes" in the same directory as the java file to be launched.

Execution examples:
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

Example includes file:
# Bring in my utilities classes
../utilities

EOF
}

#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 ]; then
echo File ${javaFilename}.java not found.
return
fi
fi

compileList=""
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

if [ $compile -eq 1 ]; then
#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 ]; then
rm ${javaFilename}.class
fi
if [ -f ${javaFilename}.jar ];then
rm ${javaFilename}.jar
fi

#Compile java file into classfile(s).
if [ -z $compileList ]; then
javac -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 ]; then
java -jar ${javaFilename}.jar $javaFilename
fi


MacBook Pro Retina SD card slot problem

I bought a 13" MacBook Pro Retina from a nice guy on craigslist a few months ago. It has been a spectacular machine in speed, capacity and light weight. SSD drives are certainly the way of the future.

One of the features of MacBooks has been the SD card slot, allowing users to plug in the memory chip from their camera or whatever, for quick and easy file transfer. Historically, I've not made use of these slots so I thought it would merely be another way water could be spilled into the machine, rather than a useful port. Since switching to a Canon Rebel t3i which uses an SD card, I have found the slot to be very handy and my preferred method of pulling photos and clearing out the camera memory.

After a few months of use however, the slot stopped working. I ignored it for a few weeks, thinking it would go back to working either through magic or the slight jarring that probably occurs when the machine rides around in my bag. When I tried it again and found it had not repaired itself, I gave it a scornful look and started messing with the seating of the SD card. I found, as I later discovered others had, that if the card is seated completely in the slot, it will not be recognized by the machine. If you pull the card out about 1-2 millimeters, the card will be recognized and work, though not consistently.

I brought the machine into the Genius Bar and they said the SD card board should be replaced. This was done under warranty because the machine is only 7 months old, but all told it would have cost $83.32. For anyone who is facing this troubling problem, see if your machine is still under warranty. If so, pop into a Mac shop and get it fixed. If you're not under warranty and want to save money, maybe you can ride out the millimeter fix until the board finally dies. Then you'll have to belly up and pay the piper or use a USB reader, which is no big deal.

Droid 4 and Webtop

The intended post was to be about an app on my phone but as my first and only post was about the G1 5 years ago, I'll provide a quick update.

The G1 was great. It's small, resilient and has the physical keyboard with separate row for number keys. Unfortunately, the "c" key basically stopped working, which is a bit of a nuisance. When that became truly painful and large improvements in the camera had come to pass, I was looking at a Droid 1 that seemed to function really well, but the writing was on the wall that it would soon be outdated. The Droid 2 arrived on the market and I got one the next day. It was exciting, though it lacked the huge crowds generated at the G1 launch years earlier.

The Droid 2 was a great phone though it lacked a separate row for the number keys, instead requiring use of the Alt modifier key. This is still a pet peeve of mine with respect to many of the physical keyboards available.

Thankfully the issue was remedied with the Droid 4, which I now use and enjoy thoroughly. Along with the separate number row, it has a micro hdmi port for use with a monitor or TV. I didn't think I'd ever have any real need for it, but I bought a cable anyway, just in case. That gambit paid off recently when I had to leave my MacBook at the shop for an SD card slot repair.

That night I dug up my micro hdmi cable, plugged the phone into my monitor and connected the bluetooth keyboard. It was excellent! Not great resolution, and the OS lacks the flexibility, speed and power of a notebook, but I could continue working on some scripting with a nice, big screen and full keyboard.

Though the need rarely comes up, I think the Webtop and hdmi port were a great addition to the Motorola Droid family of phones. Looks like subsequent models let go of this feature because, like me, few people really made use of it. Maybe if Android becomes more flexible (like being able to have more than one app on screen at a time) this technology could be in demand. In the meantime, we'll keep working with our notebook and phone or phablet combinations.