Last active
December 18, 2015 10:28
-
-
Save pforhan/5768366 to your computer and use it in GitHub Desktop.
Runs a Java source file right from a BASH shell. Remember to chmod a+x your .java!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*bin/echo Running $0 | |
filename=`basename "$0"` | |
classname=${filename%.*} | |
if [ $0 -nt /tmp/$filename/$classname.class ] | |
then | |
echo Compiling $0 | |
mkdir /tmp/$filename 2> /dev/null | |
javac -d /tmp/$filename $filename || exit | |
fi | |
java -cp /tmp/$filename $classname "$@" | |
exit | |
*/ | |
public class JavaShellScript { | |
public static void main(String[] args) throws Exception { | |
System.out.println("Hello world!"); | |
} | |
} |
echo
is a nice touch.
... and now updated with compilation date check.
Now stops the script if compilation fails.
Still needs more tweaking, should be passing $0 to javac, otherwise it won't compile unless you are in the same directory.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Small refinement of @swankjesse's gist with a header that no longer needs to be updated for every file.