The root cause of problem is
compilerOutput=$(javac Foo.java)
When you use command substitution, only the standard output get assigned to compilerOutput
For javac there is no standard output, that is the standard output is always empty, What you see on the screen in standard error
There for even when there is no errors in the program or not
if ["$compilerOutput" = ""]; then
will always be true as the standard output is always empty
Solution
The proper way of doing this is to get the exit status of the previous command through the shell variable $?
Code
#!/bin/bash
cd ~/Desktop/Foo/src
javac Foo.java 2> errors
if [ $? = "0" ]; then
java Foo
fi
Changes made
javac Foo.java 2> errors
This statement redirects bstandard error to errors file
if [ $? = "0" ]; then
Checks if the exit status of previous command, javac Foo.java is 0 (Successfull compilation)