Skip to content

Instantly share code, notes, and snippets.

@pforhan
Last active December 18, 2015 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pforhan/5768366 to your computer and use it in GitHub Desktop.
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!
/*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!");
}
}
@pforhan
Copy link
Author

pforhan commented Jun 12, 2013

Small refinement of @swankjesse's gist with a header that no longer needs to be updated for every file.

@swankjesse
Copy link

echo is a nice touch.

@pforhan
Copy link
Author

pforhan commented Jun 12, 2013

... and now updated with compilation date check.

@pforhan
Copy link
Author

pforhan commented Jun 13, 2013

Now stops the script if compilation fails.

@pforhan
Copy link
Author

pforhan commented Jun 26, 2013

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