Skip to content

Instantly share code, notes, and snippets.

@davethomas11
Created October 24, 2016 22:36
Show Gist options
  • Save davethomas11/c6a9f0f75ff7cd64f993ca289de060c0 to your computer and use it in GitHub Desktop.
Save davethomas11/c6a9f0f75ff7cd64f993ca289de060c0 to your computer and use it in GitHub Desktop.
# Dave Thomas Oct 2015
# I've added this function to my ~/.bash_profile
# It allows for quick one liner tests of java code from the command line
# Usage example:
# javai 'System.out.println("Hello, World!");'
#
# Second param can be import statements
# Eg:
# javai 'System.out.println(new Date().toString());' 'import java.util.Date;'
#
# Code is run inside static main function.
#
function javai {
if [ -z "$1" ]
then echo "Please pass some java code as the first parameter"
return
fi
TEMP_CLASS_NAME="JAVAI_$$_$(date +'%y%d%m%s')"
TEMP_FILE="/tmp/$TEMP_CLASS_NAME.java"
echo "/* Auto Gen javai File */" > $TEMP_FILE
if [ -n "$2" ]
then echo "$2" >> $TEMP_FILE
fi
echo "public class $TEMP_CLASS_NAME \
{ public static void main(String[] args) { $1 } }" >> $TEMP_FILE
javac $TEMP_FILE
cd /tmp
java $TEMP_CLASS_NAME
cd - > /dev/null
rm -rf $TEMP_FILE
rm -rf "/tmp/$TEMP_CLASS_NAME.class"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment