I spend a lot of time bouncing between different versions of JDKs and different versions of Grails. To make my life easier, I’ve got these files in my bin directory.
setJDK
#!/bin/sh cd /System/Library/Frameworks/JavaVM.framework/Versions CURJDK="`readlink CurrentJDK`" echo Current JDK version: $CURJDK if [ "$1" == "" ]; then echo Installed versions: ls exit fi VERFOUND=`ls | grep $1 | head -n 1` if [ "$VERFOUND" != "$1" ]; then BASE="`basename $0`" echo Error: Could not change JDK-- version $1 not installed! echo Run $BASE without arguments to see a list of installed versions. exit 127 fi echo You must now enter your Mac OS X password to change the JDK. sudo ln -fhsv $1 CurrentJDK sudo ln -fhsv $1 Current
I found that setJDK code somewhere on the internet, but I’m not really sure where and I had trouble finding it again. So I thought I’d share it here.
To integrate SoyLatte Java (which you probably want to be using anyway), just create a symlink in /System/Library/Frameworks/JavaVM.framework/Versions that points to your SoyLatte install.
setGrails
I’ve modified that script a bit to allow me to modify the version of Grails to use. This script assumes that you have your Grails distributions stored in BINDIR, and you’ve added $BINDIR/grails/bin to PATH. Once that’s set up, this script works like a charm:
#!/bin/sh cd $BINDIR CURGRAILS="`readlink grails`" echo Current Grails version: $CURGRAILS if [ "$1" == "" ]; then echo Installed versions: echo `ls -1 | grep "grails-"` exit fi VERFOUND=`ls -1 | grep $1 | head -n 1` if [ "$VERFOUND" != "grails-$1" ]; then BASE="`basename $0`" echo Error: Could not change Grails-- version $1 not installed! echo Run $BASE without arguments to see a list of installed versions. exit 127 fi ln -fhsv "grails-$1" grails
Finger-Saving Tips
If you’re really hardcore, add these lines to ~/.profile:
alias sg=setGrails alias sj=setJDK
Comments
- December 11, 2009, Fred wrote: Nice. Command line tooling just doesn't seem to be hotness in the Java community, so tips like this don't get shared too often, it seems to me. I like!
- December 11, 2009, Robert Fischer wrote: Too many people are too dependent on IDEs. I find more power with less annoyance in the command line. But, y'know, I started my programming life pissing people off in perl (e.g.) instead of pissing people off on the JVM, so I have an intimacy with the command line I don't find in a lot of Java or .Net developers.
- December 11, 2009, Gabe Beged-Dov wrote: Hi Robert, A similar version that separates the "default" (relink) from setversion is described in this post: http://old.nabble.com/setgrails-version-%28bash%29-script-with-completion-td26410455.html Best regards, Gabe
- December 11, 2009, Nurullah Akkaya wrote: setJDK script is from, http://www.macosxhints.com/article.php?story=20060121171126988
- December 11, 2009, Gabe Beged-Dov wrote: The setjdk script (http://docs.codehaus.org/display/ninja/setjdk) referenced in my grails-user post (see earlier comment) supports setting a default version as well as setting a per-process version. It also has command completion. Best regards, Gabe
- December 11, 2009, Charles Oliver Nutter wrote: Try Nick Sieger's "pickjdk" script...I love it. I made some modifications to allow selecting by number in one command as well: http://gist.github.com/143128
- December 11, 2009, Ted Naleid wrote: Nice! I did something similar a while ago where it switches both the groovy and grails symlinks with one command. I like to have them in sync and can never quite remember which dot version of groovy belongs with a grails version. Mine is more manual than this though because it can't interrogate what's actually installed in the directory, you need to manually add an alias pairing the particular groovy and grails versions together. Somethign I could automate with a little awk/sed magic and looking in the lib dir, but the pain hasn't quite hit that level yet :). Thanks for sharing. I love seeing other shell tricks for coding.
- December 12, 2009, Gabe Beged-Dov wrote: The problem with relinking is that it is global so you can't have several concurrent shells using different versions of the framework. That's where the distinction of setdefault vs. setversion is useful. Also very nice to have completion.
This post was by Robert Fischer, written on December 11, 2009.
Comment on this post: http://enfranchisedmind.com/blog/posts/setjdk-setgrails/#respond
Public Permalink: http://enfranchisedmind.com/blog/posts/setjdk-setgrails/

This article was a post on the EnfranchisedMind blog. EnfranchisedMind Blog by Robert Fischer, Brian Hurt, and Other Authors is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.