Skip to content

Instantly share code, notes, and snippets.

@cboddy
Last active July 4, 2018 17:15
Show Gist options
  • Save cboddy/bf045ad337281a55a876fd06feddbc39 to your computer and use it in GitHub Desktop.
Save cboddy/bf045ad337281a55a876fd06feddbc39 to your computer and use it in GitHub Desktop.
A utility for compiling and running a single file Java program (for scripting)
#!/bin/bash
# compile a java source file and run it
#
# two options:
# 1. Add to interactive environment
#
# > source jscript.sh.
# (or do the above in ~/.bashrc)
# to make the function jscript available eg.
# > jscript HelloWorld.java
#
# 2. run this script as an executable eg.
# > ./jscript.sh HelloWorld.java
#
function jscript {
javac $1 && java $(echo $1 | sed 's/.java//')
rm -f $(echo $1 | sed 's/.java/.class/')
}
if [[ $_ = $0 ]]
then
jscript $1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment